use-persisted-reducer
A custom React Hook that persist state from useReducer
use-persisted-reducer
is not a hook itself, but is a factory that accepts a storage key
and an optional storage provider (default = localStorage
) and returns a hook
that you can use as a direct replacement for useReducer
.
Features
Persists the state to localStorage
Requirement
To use use-persisted-reducer
, you must use react@16.8.0
or greater which includes Hooks.
Installation
$ npm i use-persisted-reducer --save
or
$ yarn add use-persisted-reducer
Example
;; const usePersistedReducer = ; const initialState = count: 0 ; { } { const state dispatch = ; return <> Count: statecount <button onClick= >+</button> <button onClick= >-</button> </> ;} ;
You can also pass in your own storage provider
Example
;; // defaults to globalThis.localStorageconst usePersistedReducer = ;
Contributing
- Fork it!
- Create your feature branch:
git checkout -b feature-name
- Commit your changes:
git commit -am 'Some commit message'
- Push to the branch:
git push origin feature-name
- Submit a pull request 😉😉
How can I thank you?
Why not star the github repo? I'd love the attention! Why not share the link for this repository on Twitter?
Don't forget to follow me on twitter!
Thanks! John Ayeni.
License
MIT Licensed