Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update documentation of persist #2147

Merged
merged 4 commits into from
Nov 1, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/integrations/persisting-store-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -731,3 +731,31 @@ interface BearState {
removeItem: (name) => localStorage.removeItem(name),
},
```

If writing serialization and deserialization code is tedious, you can use third-party libraries to serialize and deserialize different types of data

SuperJSON serialize data along with its type, allowing the data to be parsed back to its original type upon deserialization
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, how about adding this?

Suggested change
SuperJSON serialize data along with its type, allowing the data to be parsed back to its original type upon deserialization
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


```ts
import superjson from "superjson";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run prettier.

import { StorageValue } from "zustand/middleware";

interface BearState {
// ...
}
//...

storage: {
getItem: (name) => {
const str = localStorage.getItem(name);
if (!str) return null;
return {
state: superjson.parse<StorageValue<BearState>>(str).state,
};
},
setItem: (name, value) => {
localStorage.setItem(name, superjson.stringify(value));
},
removeItem: (name) => localStorage.removeItem(name),
}
```