import { useReducer, useEffect } from 'react'
// Usage
const initialState = {count: 0};
function reducer(state, action) {
switch (action.type) {
case 'increment':
return {count: state.count + 1};
case 'decrement':
return {count: state.count - 1};
default:
throw new Error();
}
}
function Counter() {
const [state, dispatch] = useLocallyPersistedReducer(reducer, initialState);
return (
<>
Count: {state.count}
<button onClick={() => dispatch({type: 'decrement'})}>-</button>
<button onClick={() => dispatch({type: 'increment'})}>+</button>
</>
);
}
// Hook
function useLocallyPersistedReducer(reducer, initialArg, storageKey, init = null) {
const hookVars = useReducer(reducer, initialArg, (initialArg) => {
const persisted = JSON.parse(localStorage.getItem(storageKey))
return persisted !== null
? persisted
: init !== null ? init(initialArg) : initialArg
})
useEffect(() => {
localStorage.setItem(storageKey, JSON.stringify(hookVars[0]))
}, [storageKey, hookVars[0]])
return hookVars
}
-
Notifications
You must be signed in to change notification settings - Fork 0
maxwaiyaki/use-persisted-reducer
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
useReducer but to persist state in local and session storage
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published