
Research
SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains
An emerging npm supply chain attack that infects repos, steals CI secrets, and targets developer AI toolchains for further compromise.
create-context-helper
Advanced tools
Create react context especially atomic context without painless.
With Atomic Context your component will only re-render based on atomic value changes but not the whole value you provide in a common pattern. It's simple to use and more powerful. check
$ yarn add create-context-helper
# or
$ npm i create-context-helper
This library provide createContextHelper to create a common pattern Context And createAtomicContextHelper to create atomic Context.
import { createContextHelper } from 'create-context-helper'
const [FooBarProvider, useFooBar] = createContextHelper<{
foo: number
bar: string
}>('FooBar', {
foo: '',
bar: '',
})
const App = () => {
return (
<FooBarProvider foo={'hello'} bar={'world'}>
<Comp />
</FooBarProvider>
)
}
const Comp = () => {
const { foo, bar } = useFoo()
return (
<div>
{foo} {bar} // hello world
</div>
)
}
import { createAtomicContextHelper } from 'create-context-helper'
const [FooBarProvider, useFooBar] = createAtomicContextHelper<{
foo: number
bar: string
}>('FooBar', {
foo: '',
bar: '',
})
const App = () => {
return (
<FooBarProvider foo={'hello'} bar={'world'}>
<Comp />
</FooBarProvider>
)
}
const Comp = () => {
const { foo } = useFooBar(['foo'])
return <div>{foo} // hello</div>
}
Simple as createContextHelper, but more powerful. Your context now becomes atomic which means now taking useFoo(['foo']) for value foo, the component will only change when foo changes.
createAtomicContextHelper will generate a provider tree for you like this
<FooBarProvider>
<FooBar.foo.Provider>
<FooBar.bar.Provider>
<Comp />
</FooBar.bar.Provider>
</FooBar.foo.Provider>
</FooBarProvider>
And with selector ['foo'] in hook useFooBar, the FooBarProvider will only collects FooBar.foo.Context for you. which means <Comp /> only re-render when foo changes.
export const createContextHelper = <
ContextType,
ProviderProps extends Partial<ContextType> = Partial<ContextType>,
>(
prefix: string, // prefix for provider displayName
defaultValue: ContextType, // context defaultValue
): [
React.FC<ProviderProps>, // provider
() => ContextType, // use hook
React.Context<ContextType> // context
]
export const createAtomicContextHelper = <
ContextType extends { [s: string]: any },
ProviderProps extends Partial<ContextType> = Partial<ContextType>,
K extends keyof ContextType = keyof ContextType,
>(
prefix: string,
defaultValue: ContextType,
): [
React.FC<ProviderProps>,
<T extends K[] = K[]>(selector?: T) => Pick<ContextType, T[number]>,
Record<keyof ContextType, React.Context<ContextType[string]>>,
]
There is no magic in createAtomicContextHelper, It only separates shallow values in the original Provider into smaller providers. Inside createAtomicContextHelper, it calls useContext conditional depends on selector. So dynamic selector is not allowed. if don't, it will cause a serious render problem(createAtomicContextHelper handles this scenario by using the first selector).
FAQs
Create react context helper without painless
We found that create-context-helper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.

Research
An emerging npm supply chain attack that infects repos, steals CI secrets, and targets developer AI toolchains for further compromise.

Company News
Socket is proud to join the OpenJS Foundation as a Silver Member, deepening our commitment to the long-term health and security of the JavaScript ecosystem.

Security News
npm now links to Socket's security analysis on every package page. Here's what you'll find when you click through.