Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/realdennis/persist-hook
Persist Hook : Give me a key, I'll make your React Hook's state persistence.
https://github.com/realdennis/persist-hook
hooks localstorage persistence persistent react state-persistence
Last synced: 3 months ago
JSON representation
Persist Hook : Give me a key, I'll make your React Hook's state persistence.
- Host: GitHub
- URL: https://github.com/realdennis/persist-hook
- Owner: realdennis
- License: mit
- Created: 2019-02-16T05:28:55.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T18:48:14.000Z (about 2 years ago)
- Last Synced: 2024-09-26T20:49:10.105Z (4 months ago)
- Topics: hooks, localstorage, persistence, persistent, react, state-persistence
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/persist-hook
- Size: 563 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Persist Hook
[Demo](https://codesandbox.io/s/0p5kx9rqrl)
> Give me a key, I'll make your hook's state persistence.## Installation
```sh
$ npm install persist-hook
```## Usage
```jsx
import PersistHook from "persist-hook";const config = { key: "$$$normalhook" };
const usePersistState = (initialValue = 0) => {
const { setPersist, getPersist } = PersistHook(config);
const [state, setState] = useState(getPersist(initialValue));
setPersist(state); // it will synchronize state and localstorage
return [state, setState];
};const App = () => {
const [state, setState] = usePersistState(0);
return (
{state}
setState(state + 1)}>+1
setState(state - 1)}>-1
window.location.reload()}>Reload this page
);
};
```