g-store
A Simplified Global store for React
Installation
npm install g-store
Usage
In the index.js file of the react app
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import {Provider} from 'g-store';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<Provider>
<App />
</Provider>
);
In You can use it in your Component
import { useGStore } from 'g-store';
function App() {
const [state, setState] = useGStore(0,"UniqueGlobalStateName");
const increment1=()=>{
const tempState=state+1
setState(tempState)
}
return (
<>
<button onClick={increment}>increment</button>
<div >{state}</div>
</>
);
}
export default App;
Limitation
-
You can use setState of useGStore to update the state only like this
const tempState=state+1 setState(tempState)
You can't use like below
setState((state)=>state+1)
-
You have to pass a unique name to each state as a second argument in useGStore
const [userName, setUserName] = useGStore("initial Value","userName"); const [age, setAge] = useGStore(20,"age");
License
MIT