
Security News
Feross on TBPN: How North Korea Hijacked Axios
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.
use-root-reducer
Advanced tools
a helper to create and maintain a global state without redux. (based on react hook)
A helper to create and maintain a global state without redux. (based on react hooks)
$ npm i use-root-reducer --save
First of all, create a global state and a global dispatch function in your root component:
// app.jsx
import React, { useReducer } from "react";
import useRootReducer from "use-root-reducer";
import { fooReducer, barReducer } from "./reducer";
import { StateContext, DispatchContext } from "./context";
const App = ({ children }) => {
const [state, dispatch] = useRootReducer({
foo: useReducer(fooReducer, "foo"),
bar: useReducer(barReducer, "bar")
});
return (
<DispatchContext.Provider value={dispatch}>
<StateContext.Provider value={state}>{children}</StateContext.Provider>
</DispatchContext.Provider>
);
};
export default App;
You can pass your global state and global dispatch method (it will have foo and bar key-value in the above example) to your children components via props or with React Context API.
Second, it's recommended to maintain your context code in a independent file:
// context.jsx
import React from "react";
export const StateContext = React.createContext({});
export const DispatchContext = React.createContext(null);
export const OtherContext = React.createContext();
In the end, in your child components you can access your global state and global dispatch with useContext:
//
import React from "react";
import { StateContext, DispatchContext } from "./context.js";
export default () => {
const state = React.useContext(StateContext);
const dispatch = React.useContext(DispatchContext);
const { foo, bar } = state;
return (
<button
onClick={() => {
// dispatch your action and will be received in all your reducers
dispatch({ type: "update", payload: "foo updated", meta: "foo" });
}}
>{`${foo} and ${bar}`}</button>
);
};
FAQs
a helper to create and maintain a global state without redux. (based on react hook)
The npm package use-root-reducer receives a total of 30 weekly downloads. As such, use-root-reducer popularity was classified as not popular.
We found that use-root-reducer 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
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.

Security News
OpenSSF has issued a high-severity advisory warning open source developers of an active Slack-based campaign using impersonation to deliver malware.

Research
/Security News
Malicious packages published to npm, PyPI, Go Modules, crates.io, and Packagist impersonate developer tooling to fetch staged malware, steal credentials and wallets, and enable remote access.