Skip to content

Commit

Permalink
fixed error in persist example and added typescript (#2978)
Browse files Browse the repository at this point in the history
* fixed error in persist example and added typescript

* chore: formatted code

---------

Co-authored-by: sidahmedabdelillah <[email protected]>
  • Loading branch information
sidahmedabdelillah and sidahmedabdelillah authored Jan 28, 2025
1 parent 5a886cb commit bc3e94e
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion docs/integrations/persisting-store-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,32 @@ for more details.
import { create } from 'zustand'
import { persist, createJSONStorage } from 'zustand/middleware'

export const useBearStore = create(
export const useBearStore = create()(
persist(
(set, get) => ({
bears: 0,
addABear: () => set({ bears: get().bears + 1 }),
}),
{
name: 'food-storage', // name of the item in the storage (must be unique)
storage: createJSONStorage(() => sessionStorage), // (optional) by default, 'localStorage' is used
},
),
)
```

## Typescript simple example

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

type BearStore = {
bears: number
addBear: () => void
}

export const useBearStore = create<BearStore>()(
persist(
(set, get) => ({
bears: 0,
Expand Down

0 comments on commit bc3e94e

Please sign in to comment.