Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/roadmanfong/zustand-persist
Persist and rehydrate state
https://github.com/roadmanfong/zustand-persist
react react-native zustand
Last synced: 14 days ago
JSON representation
Persist and rehydrate state
- Host: GitHub
- URL: https://github.com/roadmanfong/zustand-persist
- Owner: roadmanfong
- License: mit
- Created: 2020-08-31T14:19:07.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2023-07-23T13:02:03.000Z (over 1 year ago)
- Last Synced: 2024-10-15T00:21:26.609Z (30 days ago)
- Topics: react, react-native, zustand
- Language: JavaScript
- Homepage:
- Size: 1.56 MB
- Stars: 118
- Watchers: 3
- Forks: 13
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
⚠️ This project is no longer being maintained ⚠️
Zustand now has built in persistence middleware, so has persist functionality out of the box. Read more in the [Zustand readme](https://github.com/pmndrs/zustand#persist-middleware).
# Zustand persist
Persist and rehydrate state works on react and react native.
[Zustand](https://github.com/react-spring/zustand) is a small, fast and scaleable bearbones state-management solution.
This library is inspired by [https://github.com/rt2zz/redux-persist](https://github.com/rt2zz/redux-persist).
Contributions are Welcome
[![Build Status](https://travis-ci.org/roadmanfong/zustand-persist.svg?branch=master)](https://travis-ci.org/roadmanfong/zustand-persist) [![npm version](https://img.shields.io/npm/v/zustand-persist.svg?style=flat-square)](https://www.npmjs.com/package/zustand-persist) [![npm downloads](https://img.shields.io/npm/dm/zustand-persist.svg?style=flat-square)](https://www.npmjs.com/package/zustand-persist)
```cli
$ npm install zustand-persist
$ yarn add zustand-persist
```Try [Demo](https://codesandbox.io/s/zustand-persist-example-5u3ry?file=/src/App.js) Here
## Middleware
```ts
// if in react native
// import AsyncStorage from '@react-native-community/async-storage'const { persist, purge } = configurePersist({
storage: localStorage, // use `AsyncStorage` in react native
rootKey: 'root', // optional, default value is `root`
})const useStore = createStore(
persist({
key: 'auth', // required, child key of storage
allowlist: ['isAuthenticated', 'user'], // optional, will save everything if allowlist is undefined
denylist: [], // optional, if allowlist set, denylist will be ignored
}, (set) => ({
isAuthenticating: false,
isAuthenticated: false,
user: undefined,
login: async (username, password) =>{
set((state) => ({ isAuthenticating: true }))
const { user } = await webLogin(username, password)
set((state) => ({
isAuthenticated: true,
isAuthenticating: false,
user,
})),
}
}))
)
```## PersistGate
```tsx
function App() {
// Must call useStore to bootstrap persistence or will stop on loading screen
useStore()
return (
{
console.log('onBeforeLift')
}}
loading={}>
)
}
```## Limitations
Zustand allows any valid JavaScript data types to set in the store, such as Set, Map, or functions. Both `localStorage` and `AsyncStorage` require that all values set in them be serializable, or converted into string form for storage. This means if you wish to persist the contents of your store all of its values need to be serializable. Any values which are not serializable will be silently absent upon hydration.A note on storing actions (functions): your actions are safe as long as they exist in the initial state during store creation. The hydration process merges initial state with stored state.
## Todo
- enhanced removing root logic