Skip to content

Commit

Permalink
feat(middleware/persist): new storage option (#1463)
Browse files Browse the repository at this point in the history
* feat(middleware/persist): new storage option

* fix typo
  • Loading branch information
dai-shi committed Jan 1, 2023
1 parent 633c7dd commit 1e986ec
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 65 deletions.
63 changes: 6 additions & 57 deletions docs/integrations/persisting-store-data.md
Expand Up @@ -19,7 +19,7 @@ for more details.

```ts
import create from 'zustand'
import { persist } from 'zustand/middleware'
import { persist, createJSONStorage } from 'zustand/middleware'

export const useBearStore = create(
persist(
Expand All @@ -29,7 +29,7 @@ export const useBearStore = create(
}),
{
name: 'food-storage', // name of the item in the storage (must be unique)
getStorage: () => sessionStorage, // (optional) by default, 'localStorage' is used
storage: createJSONStorage(() => sessionStorage), // (optional) by default, 'localStorage' is used
}
)
)
Expand All @@ -44,7 +44,7 @@ The given name is going to be the key
used to store your Zustand state in the storage,
so it must be unique.

### `getStorage`
### `storage`

> Type: `() => StateStorage`
Expand All @@ -54,75 +54,24 @@ The `StateStorage` can be imported with:
import { StateStorage } from 'zustand/middleware'
```

> Default: `() => localStorage`
> Default: `createJSONStorage(() => localStorage)`
Enables you to use your own storage.
Simply pass a function that returns the storage you want to use.

Example:

```ts
export const useBoundStore = create(
persist(
(set, get) => ({
// ...
}),
{
// ...
getStorage: () => AsyncStorage,
}
)
)
```

### `serialize`

> Type: `(state: Object) => string | Promise<string>`
> Default: `(state) => JSON.stringify(state)`
The only way to store an object in a storage is as a string.
If the default method of serialization doesn't suit your needs,
pass custom functions for serialization
and [deserialization](#deserialize) (see below).

For example, if you want to store your state in base64:

```ts
export const useBoundStore = create(
persist(
(set, get) => ({
// ...
}),
{
// ...
serialize: (state) => btoa(JSON.stringify(state)),
}
)
)
```

### `deserialize`

> Type: `(str: string) => Object | Promise<Object>`
import { persist, createJSONStorage } from 'zustand/middleware'

> Default: `(str) => JSON.parse(str)`
If you pass a custom [`serialize`](#serialize) function,
you will most likely need to pass a custom deserialize function as well.

To continue the example above,
you could deserialize the base64 value using the following:

```ts
export const useBoundStore = create(
persist(
(set, get) => ({
// ...
}),
{
// ...
deserialize: (str) => JSON.parse(atob(str)),
storage: createJSONStorage(() => AsyncStorage),
}
)
)
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -310,7 +310,7 @@ You can persist your store's data using any kind of storage.
```jsx
import create from 'zustand'
import { persist } from 'zustand/middleware'
import { persist, createJSONStorage } from 'zustand/middleware'

const useFishStore = create(
persist(
Expand All @@ -320,7 +320,7 @@ const useFishStore = create(
}),
{
name: 'food-storage', // unique name
getStorage: () => sessionStorage, // (optional) by default, 'localStorage' is used
storage: createJSONStorage(() => sessionStorage), // (optional) by default, 'localStorage' is used
}
)
)
Expand Down

0 comments on commit 1e986ec

Please sign in to comment.