react-ezglobalstate-hook
v0.1.4
Published
A reactive state with localstorage and SWR
Downloads
8
Readme
React-EZGlobalState-Hook
A reactive state with localstorage and SWR.
Reference
npm i --save react-ezglobalstate-hook
oryarn add react-ezglobalstate-hook
import {
useGlobalState,
setGlobalState
} from 'react-ezglobalstate-hook';
useGlobalState(key: string, initialValue: any):
const myState = useGlobalState("unique_key", "default");
setGlobalState(key: string, value: any):
setGlobalState("unique_key", "value");
Example
import {
useGlobalState,
setGlobalState
} from 'react-ezglobalstate-hook';
export default function Component(){
//Is this case, Hello World is the primary value if we
//never had stored this value before.
const myState = useGlobalState("myState", "Hello World");
function handleChange(){
setGlobalState("myState", "Good Bye World");
}
return (
<div>
<span>{`Actual value for myState is: ${myState}`}</span>
<button onClick={handleChange}>Change Name</button>
</div>
)
}
License
MIT