Skip to content

Commit eb37b28

Browse files
committed
docs: Update variable name to example name and separate storage as const
1 parent 84f202d commit eb37b28

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

docs/integrations/persisting-store-data.md

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -635,30 +635,47 @@ If you're using a type that JSON.stringify() doesn't support, you'll need to wri
635635
For example, [Superjson](https://github.com/blitz-js/superjson) can serialize data along with its type, allowing the data to be parsed back to its original type upon deserialization
636636

637637
```ts
638-
import superjson from "superjson"; // can use anything: serialize-javascript, devalue, etc.
639-
import { StorageValue } from "zustand/middleware";
638+
import superjson from 'superjson' // can use anything: serialize-javascript, devalue, etc.
639+
import { PersistStorage } from 'zustand/middleware'
640640

641641
interface BearState {
642-
...: Map<string, string>;
643-
...: Set<string>;
644-
...: Date,
645-
...: RegExp;
642+
bear: Map<string, string>
643+
fish: Set<string>
644+
time: Date
645+
query: RegExp
646646
}
647-
//...
648647

649-
storage: {
650-
getItem: (name) => {
651-
const str = localStorage.getItem(name);
652-
if (!str) return null;
653-
return {
654-
state: superjson.parse<StorageValue<BearState>>(str).state,
655-
};
656-
},
657-
setItem: (name, value) => {
658-
localStorage.setItem(name, superjson.stringify(value));
659-
},
660-
removeItem: (name) => localStorage.removeItem(name),
661-
}
648+
const storage: PersistStorage<BearState> = {
649+
getItem: (name) => {
650+
const str = localStorage.getItem(name)
651+
if (!str) return null
652+
return superjson.parse(str)
653+
},
654+
setItem: (name, value) => {
655+
localStorage.setItem(name, superjson.stringify(value))
656+
},
657+
removeItem: (name) => localStorage.removeItem(name),
658+
}
659+
660+
const initialState: BearState = {
661+
bear: new Map(),
662+
fish: new Set(),
663+
time: new Date(),
664+
query: new RegExp(''),
665+
}
666+
667+
export const useBearStore = create<BearState>()(
668+
persist(
669+
(set) => ({
670+
...initialState,
671+
// ...
672+
}),
673+
{
674+
name: 'food-storage',
675+
storage,
676+
}
677+
)
678+
)
662679
```
663680

664681
### How can I rehydrate on storage event

0 commit comments

Comments
 (0)