Skip to content

Commit cba2dc4

Browse files
committed
avoid 'any' errors and make example clearer
1 parent f11cc7f commit cba2dc4

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

docs/integrations/persisting-store-data.md

+15-4
Original file line numberDiff line numberDiff line change
@@ -698,17 +698,28 @@ Let's say your state uses `Map` to handle a list of `transactions`,
698698
then you can convert the Map into an Array in the storage prop:
699699

700700
```ts
701+
702+
interface BearState {
703+
.
704+
.
705+
.
706+
transactions: Map<any>
707+
}
708+
701709
storage: {
702710
getItem: (name) => {
703-
const str = localStorage.getItem(name)
711+
const str = localStorage.getItem(name);
712+
if (!str) return null;
713+
const { state } = JSON.parse(str);
704714
return {
705715
state: {
706-
...JSON.parse(str).state,
707-
transactions: new Map(JSON.parse(str).state.transactions),
716+
...state,
717+
transactions: new Map(state.transactions),
708718
},
709719
}
710720
},
711-
setItem: (name, newValue) => {
721+
setItem: (name, newValue: StorageValue<BearState>) => {
722+
// functions cannot be JSON encoded
712723
const str = JSON.stringify({
713724
state: {
714725
...newValue.state,

0 commit comments

Comments
 (0)