Skip to content

Commit bc3e94e

Browse files
sidahmedabdelillahsidahmedabdelillah
andauthored
fixed error in persist example and added typescript (#2978)
* fixed error in persist example and added typescript * chore: formatted code --------- Co-authored-by: sidahmedabdelillah <[email protected]>
1 parent 5a886cb commit bc3e94e

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

docs/integrations/persisting-store-data.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,32 @@ for more details.
2121
import { create } from 'zustand'
2222
import { persist, createJSONStorage } from 'zustand/middleware'
2323

24-
export const useBearStore = create(
24+
export const useBearStore = create()(
25+
persist(
26+
(set, get) => ({
27+
bears: 0,
28+
addABear: () => set({ bears: get().bears + 1 }),
29+
}),
30+
{
31+
name: 'food-storage', // name of the item in the storage (must be unique)
32+
storage: createJSONStorage(() => sessionStorage), // (optional) by default, 'localStorage' is used
33+
},
34+
),
35+
)
36+
```
37+
38+
## Typescript simple example
39+
40+
```ts
41+
import { create } from 'zustand'
42+
import { persist, createJSONStorage } from 'zustand/middleware'
43+
44+
type BearStore = {
45+
bears: number
46+
addBear: () => void
47+
}
48+
49+
export const useBearStore = create<BearStore>()(
2550
persist(
2651
(set, get) => ({
2752
bears: 0,

0 commit comments

Comments
 (0)