![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
better-context
Advanced tools
Tiny React context wrapper for using contexts without the hassle
npm i better-context
or with yarn:
yarn add better-context
Create a context
const useCoolContext = betterContext()(({ state, provided }) => {
const counter = useState(0);
return {
foo: "bar",
counter: state(counter), // using the state wrapper is optional
output: "foo bar " + provided, // "foo bar baz"
};
});
Provide it at highest common ancestor
function App() {
return (
<useCoolContext.Provider provide="baz">
<Component />
</useCoolContext.Provider>
);
}
Consume the context
function Component() {
const { counter } = useCoolContext();
// counter.value (get) ✅
// counter.value = 123 ✅
// counter.value++ ✅
// counter.set(prev => prev + 1) ✅
return (
<>
<button onClick={() => counter.value--}>-</button>
{counter.value}
<button onClick={() => counter.value++}>+</button>
</>
);
}
It's that simple!
The reason for using currying to instantiate the better context is because TypeScript as of now does not support partial type argument inference. Thus, to circumvent this issue currying is utilized to allow the context return value to be inferred while the provided value can be given or left unused:
type Provided = {
foo: string;
bar: number;
};
const useTsContext = betterContext<Provided>()(({ provided }) => {
const counter = useState(0);
return `${provided.foo}: ${provided.bar}`;
});
/* ... */
<useTsContext.Provider
provide={{
foo: "baz",
bar: 1337,
}}
>
...
</useTsContext.Provider>;
FAQs
Tiny React context wrapper for using contexts without the hassle
The npm package better-context receives a total of 1 weekly downloads. As such, better-context popularity was classified as not popular.
We found that better-context 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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.