
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-global-state-manager
Advanced tools
Create global state manager utilizing React Hooks API. (No Redux!)
npm install react-global-state-managerUse GlobalStateManager to initiate global state manger for your app.
import React from 'react';
import ReactDOM from 'react-dom';
import { GlobalStateManager } from 'react-global-state-manager';
const App = () => {
// Create Global State Manager
const state = GlobalStateManager({ apple: 5, orange: 10 });
return (
<>
<h1>Fruit Vendor</h1>
<table>
<tr>
<th>Fruit</th>
<th>Cost</th>
</tr>
<tr>
<td>Apple</td>
<td>{state.apple}</td>
</tr>
<tr>
<td>Orange</td>
<td>{state.orange}</td>
</tr>
</table>
</>
);
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
useGlobalState allows you to grab specific state in GSM and subscribe your container to listen to the changes to the requested state.
...
const Apple = () => {
const price = useGlobalState('apple');
return (
<tr>
<td>Apple</td>
<td>{ price }</td>
</tr>
)
}
const Orange = () => {
const price = useGlobalState('orange');
return (
<tr>
<td>Orange</td>
<td>{ price }</td>
</tr>
)
}
const Fruit = () => {
const state = GlobalStateManager({ apple: 5, orange: 10 });
return (
<>
<h1>Fruit Vendor</h1>
<table>
<tr>
<th>Fruite</th>
<th>Cost</th>
</tr>
<tbody>
<Apple />
</tbody>
<tbody>
<Orange />
</tbody>
</table>
</>
)
}
...
Create a new action which will trigger state update.
...
const Fruit = () => {
const state = GlobalStateManager({ apple: 5, orange: 10 });
addAction('changeApple', (state, newPrice) => {
return { apple: newPrice };
});
return (
<>
<h1>Fruit Vendor</h1>
<table>
<tr>
<th>Fruite</th>
<th>Cost</th>
</tr>
<tbody>
<Apple />
</tbody>
<tbody>
<Orange />
</tbody>
</table>
</>
)
}
...
Get a named action from GSM. Performing this action will trigger all components subscribed to this state to be re-rendered.
...
const Apple = () => {
const [newPrice, setNewPrice] = useState('');
const price = useGlobalState('apple');
const changePrice = getAction('changeOrange');
return (
<>
<tr>
<td>Apple</td>
<td>{ price }</td>
</tr>
<div style={{'position': 'aboslute', 'right': '0'}}>
<input type='text' value={newPrice} placeholder='New Price' onChange={(e) => setNewPrice(e.target.value)} />
<button onClick={()=>changePrice(parseInt(newPrice))}>Change</button>
</div>
</>
)
}
const Orange = () => {
const [newPrice, setNewPrice] = useState('');
const price = useGlobalState('orange');
const changePrice = getAction('changeApple');
return (
<>
<tr>
<td>Orange</td>
<td>{ price }</td>
</tr>
<div style={{'position': 'aboslute', 'right': '0'}}>
<input type='text' value={newPrice} placeholder='New Price' onChange={(e) => setNewPrice(e.target.value)} />
<button onClick={()=>changePrice(parseInt(newPrice))}>Change</button>
</div>
</>
)
}
const Fruit = () => {
const state = GlobalStateManager({ apple: 5, orange: 10 });
addAction('changeApple', (state, newPrice) => {
return { apple: newPrice };
});
addAction('changeOrange', (state, newPrice) => {
return { orange: newPrice };
});
return (
<>
<h1>Fruit Vendor</h1>
<table>
<tr>
<th>Fruite</th>
<th>Cost</th>
</tr>
<tbody>
<Apple />
</tbody>
<tbody>
<Orange />
</tbody>
</table>
</>
)
}
...
In this example, apple vendor can change the price of an orange and orange vendor can change the price of an apple.
Introduce side effect to be ran when desired state is being updated.
...
const Fruit = () => {
const state = GlobalStateManager({ apple: 5, orange: 10 });
...
// set side effect for apple.
addSideEffect((state) => {
console.log('You can't mess with me! I add 5 more!');
return { apple: state.apple + 5}
}, 'apple');
// set side effect for multiple states.
addSideEffect((state) => {
console.log('Price changed in fruits');
return state
}, 'apple', 'orange');
...
}
In progress. addState will add a state from anywhere in your app, and will subscribe current container to listen to the changes.
You can only use newly added state from children of current container.
If you try to useGlobalState from any other container, it will throw an error.
FAQs
Global state manager for ReactJS using React Hooks API
We found that react-global-state-manager 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

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.