
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
React context-based state management, simplified
npm i contextize
In React, a common pattern is to have:
Contextize simplifies this pattern by separating:
Let's say we're managing a boolean state. In a typical React app, we'd have to:
A) Vanilla React
import { createContext, useContext, useState } from 'react'
const ctx = createContext({})
const useCtx = () => useContext(ctx)
function Provider({ children }) {
const [state, setState] = useState()
return <ctx.Provider value={[state, setState]} children={children} />
}
Notice that useState() is the only non-boilerplate code. Everything else is the same.
B) Contextize
In contextize, we only define a hook that returns the context value. Wrapping it in a provider is handled by contextize:
import { contextize } from 'contextize'
function useController() {
return useState(false)
}
const ctx = contextize(useController)
The ctx object gives access to both the provider and the hook:
Example Usage
function Component() {
const [state, setState] = ctx.use()
...
}
function App() {
return (
<ctx.Provider>
<Component />
</ctx.Provider>
)
}
Another common pattern is to have a tagged state. Depending on the tag, we render different components. For example:
export type State = {
tag: 'loading' | 'error'
} | {
tag: 'loaded'
data: string
}
In a typical React app, we'd have to:
A) Vanilla React
const ctx = createContext({})
const useCtx = () => useContext(ctx)
function Provider({ children }) {
const [state, setState] = useState<State>({ tag: 'loading' })
useEffect(() => { ... }, [])
return <ctx.Provider value={state} children={children} />
}
function Loaded({ data }) {
return <p>{data}</p>
}
function Switcher() {
const state = useCtx()
if (state.tag === 'loading') return <p>Loading...</p>
if (state.tag === 'error') return <p>Error :/</p>
return <Loaded data={state.data} />
}
function App() {
return (
<Provider>
<Switcher />
</Provider>
)
}
Notice that:
useState() and useEffect() are non-boilerplateB) Contextize
In Contextize, we only need to define a hook returning the (tagged) context value. For switching, we use the provided guard components.
import { tagged } from 'contextize'
function useController(): State {
const [state, setState] = useState<State>({ tag: 'loading' })
useEffect(() => { ... }, [])
return state
}
const ctx = tagged(useController)
Like before, ctx contains both the provider and hook. But it also includes:
ctx.Loading, ctx.Error, ctx.Loadedctx.useLoading, ctx.useError, ctx.useLoadedExample Usage
function Loaded() {
const { data } = ctx.useLoaded()
// ...
}
function App() {
return (
<ctx.Provider>
<ctx.Loading><p>Loading...</p></ctx.Loading>
<ctx.Error><p>Error :/</p></ctx.Error>
<ctx.Loaded><Loaded /></ctx.Loaded>
</ctx.Provider>
)
}
Note that these are completely typed.
If we added a weirdState tag, we'd get ctx.WeirdState and ctx.useWeirdState (and typescript would know about it).
FAQs
React context-based state management, simplified
We found that contextize demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.