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 1 commit
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
29 changes: 29 additions & 0 deletions docs/integrations/persisting-store-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -731,3 +731,32 @@ interface BearState {
removeItem: (name) => localStorage.removeItem(name),
},
```

If writing serialisation and deserialisation code is tedious, you can use third-party libraries to serialise and deserialise different types of data.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use the American English spelling — serialization for this and other occurrences.


SuperJSON serialises data along with its type, allowing the data to be parsed back to its original type upon deserialisation
Copy link
Collaborator

Choose a reason for hiding this comment

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

It depends whether @dai-shi and other contributors want to encourage using another library for this use case in the official Zustand docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we change it to how to use persist with a custom serialization library? Like "How can I use a custom storage engine"?
스크린샷 2023-10-26 01-13-24

If you use a lot of variables like Map, Set, Date, etc. that JSON.stringify() doesn't support, it would be nice to have a way in the documentation to avoid writing your own serialization/deserialization code

No problem using other libraries (ex) serialize-javascript, devalue , ...etc)

Copy link
Collaborator

Choose a reason for hiding this comment

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

@cheatkey I'm down with the idea of having this under "How can I use a custom storage engine?".


```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),
}
```
Loading