Skip to content

Commit

Permalink
doc: add persit middleware to the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
AnatoleLucet committed Nov 7, 2020
1 parent 521aa48 commit cced1a6
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,32 @@ const immer = <T extends State>(
</details>
</overview>
## Persist middleware
You can persist you store's data using any kind of storage.
```jsx
import create from "zustand"
import { persist } from "zustand/middleware"

export const useStore = create(persist(
(set, get) => ({
fish: 0,
addAFish: () => set({ fish: get().fish + 1 })
}),
{
name: "food-storage", // unique name
storage: sessionStorage, // (optional) default is 'localStorage'
serialize: (state) => { // (optional) default is 'JSON.stringify'
// some stuff to serialize the state and return it
},
deserialize: (str) => { // (optional) default is 'JSON.parse'
// some stuff to deserialize the state and return it
}
}
))
```
## Can't live without redux-like reducers and action types?
```jsx
Expand Down

0 comments on commit cced1a6

Please sign in to comment.