devtools with option serialize.immutable does not deserialize state from devtools correctly (broken redux devtools "Jump" / "Revert" etc.)
#2561
Replies: 3 comments 9 replies
|
@JayFoxRox Thanks for reporting! |
|
Root cause: Fix — thread the reviver into + const reviver = (options as {
+ serialize?: { reviver?: (key: string, value: any) => any }
+ }).serialize?.reviver
@@ parseJsonThen
-const parseJsonThen = <T>(stringified, fn) => {
- parsed = JSON.parse(stringified)
+const parseJsonThen = <T>(stringified, fn, reviver?) => {
+ parsed = JSON.parse(stringified, reviver)…and pass Verified: full suite green (215/215) plus a regression test in Scope: this honors a user-supplied I couldn't open a PR (PR creation looks limited to collaborators right now) — glad to submit it if that opens up, or you're welcome to pull the branch. |
|
I put together a fix for this — verified against the current Root cause confirmed: Fix: thread Added 3 regression tests in I can't open a PR directly ( Diff: koreahghg/zustand@main...fix/devtools-serialize-reviver Happy to adjust if a maintainer wants a different approach. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Summary
redux-devtools provides a
serializeoption: https://github.com/reduxjs/redux-devtools/blob/main/extension/docs/API/Arguments.md#serialize.You can provide the
immutable-jslibrary to theserialize.immutablefield to: "automatically serialize/deserialize immutablejs via remotedev-serialize."This works fine for serializing: redux-devtools will show
ImmutableSetif zustand store was created with this option.However, it breaks with deserializing during
JUMP_TO_ACTION,REVERTand other devtools options, when used with zustand.With zustand, the state doesn't get deserialized correctly, so the next time the store is used, the code will receive the wrong data type.
This problem does not occur with redux.
I believe this should work:
serializeoption was added in Allow optional REDUX DEVTOOLS "options" object #540 by @marcoSvenLink to reproduction
https://github.com/JayFoxRox/zustand/tree/immutable-bug/examples/demo
All changes are in this commit: JayFoxRox@80f0765
specifically, this is using:
My reproduction is based on the zustand demo.
To show the problem, the number counter has been replaced by a growing
Immutable.Set.To display it, I
JSON.stringifytheImmutable.Set(Immutable handles JSON stringification, so it shows as array).The demo creates 2 stores; one for redux and one for zustand; each with devtools.
The counter button has been duplicated:
The stores both work until the devtools are used to jump.
When jumping, the redux store continues to work, but the zustand store breaks.
Once the state is broken, JSON stringification shows a JS object which represents the serialized JSON encoding in the redux-devtools.
Here's a video:
zustand-devtools-bug.mp4
Check List
Please do not ask questions in issues.
Please include a minimal reproduction.
Please check this if you're filing an issue regarding TypeScript.
createis to be used ascreate<T>()(...)and notcreate<T>(...).All reactions