- This React Hook lets you store the state in localStorage and sync it in between tabs, automagically.
npm install use-sync-set-state- This React Hook lets you store the state in localStorage and sync it in between tabs, automagically.
- use-sync-set-state is a superset of useState i.e, you can always set the state explicitly or via callback function, your call.
- What's difference is that it stores the state in the localStorage and syncs it between tabs by listening to localStorage's storage event.
``sh`
yarn add use-sync-set-state
or
`sh
npm i use-sync-set-state
`
`js
import useSyncSetState from 'use-sync-set-state';
const App = () => {
React.useEffect(() => {
setTheme('light');
}, [])
const [theme, setTheme] = useSyncSetState('theme', 'dark');
return
{ theme }
// changes will be reflected in all the tabs, without reload.
{localStorage.getItem('theme')}