This hook is inspired by the useState hook and makes it easy to create a reactive state variable that remains in sync with value stored in localStorage.
function Component() {
const [count, setCount] = useLocalStorageState('counter', { initialState: 0 });
return (
<>
Count: {state.count}
<button onClick={() => setCount(count + 1)}>+</button>
<button onClick={() => setCount((c) => c - 1)}>-</button>
</>
);
}const [state, setState] = useLocalStorageState(key);
const [state, setState] = useLocalStorageState(key, { initialState });
const [state, setState] = useLocalStorageState(key, { initialState, sync: true });
const [state, setState] = useLocalStorageState(key, { initialState, sync: false });This hook accepts:
key— key for localStorage,optionsobject:initialState— initial value of the state if there is no value underkeyin localStorage. If some value is there in localStorage underkey, then that is used. (default:undefined)sync— flag to keep state in sync with the other hooks with the same localStoragekey. (default:true)
This hook returns a pair of:
- current state,
setState— method to update the state.