nedux-persist
Version:
the next redux state management
71 lines (47 loc) • 2.71 kB
Markdown
# nedux-persist - persist your nedux store
 [](https://www.npmjs.com/package/nedux-persist) [](https://www.npmjs.com/package/nedux-persist)
> The official [middleware](https://github.com/lucasmrdt/nedux#%EF%B8%8F-middlewares) persistor for [nedux](https://github.com/lucasmrdt/nedux). Performant and flexible.
## 📦 Installation
```bash
npm install nedux-persist --save
```
## 💻 Usage with examples
| Name | Codesandbox |
| :-----------------: | :----------------------------------------------------------------------------------------: |
| 🔒 Persisting Token | [here](https://codesandbox.io/s/new-feather-777tm?fontsize=14&hidenavigation=1&theme=dark) | [here](https://codesandbox.io/s/nedux-todos-nm8j0?fontsize=14&hidenavigation=1&theme=dark) |
## 📜 Documentation
### `Import`
```javascript
// ES6
import { persistKeys } from 'nedux-persist';
// ES5
var persistKeys = require('nedux-persist').persistKeys;
```
### `persistKeys(keys, [localStorage])`
Create a middleware that may your `keys` persisted.
| argument | required | type | description |
| :------------: | :------: | :-----------------: | :-------------------------------- |
| `keys` | ✅ | `string[]` | The keys that you want to persist |
| `localStorage` | ❌ | [Storage](#storage) | The keys that you want to persist |
<a id="storage"></a>
### Storage
| argument | required | type | description |
| :-------: | :------: | :------------------------------------: | :------------------------- |
| `getItem` | ✅ | `(key: string) => string` | Get the value from Storage |
| `setItem` | ✅ | `(key: string, value: string) => void` | Set the value into Storage |
## 🎛 Basic Usage
```typescript
import { createStore } from 'nedux';
import { persistKeys } from 'nedux-persist';
type Store = {
token: string;
useless: number;
};
const store = createStore<Store>(
{ token: '', useless: 0 },
[persistKeys(['token'])],
);
store.subscribe('token', { next: token => console.log(`token: ${token}`) });
```
## 🙋🏼 Contributions
All [Pull Requests](https://github.com/lucasmrdt/nedux-persist/compare?expand=1), [Issues](https://github.com/lucasmrdt/nedux-persist/issues) and [Discussions](https://github.com/lucasmrdt/nedux-persist/issues) are welcomed !