-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from 1 commit
8fdfe3a
0bb6035
84f202d
eb37b28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
||
SuperJSON serialises data along with its type, allowing the data to be parsed back to its original type upon deserialisation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"? 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
} | ||
``` |
There was a problem hiding this comment.
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.