Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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

);
};
```