Putting dom reference into store will cause lag #2079
Replies: 3 comments
-
|
Can anyone look into this? |
Beta Was this translation helpful? Give feedback.
-
|
It should because of the {
name: "bear",
stateSanitizer: (state) => {
return JSON.parse(
JSON.stringify(state, (_key, value) => {
if (typeof value === "function") return undefined;
if (value instanceof HTMLElement)
return `${value.tagName} ${value.className}`;
if (value instanceof Date) return value.toLocaleString();
return value;
})
);
},
}
The root cause should be related to facebook/react#24360 HTMLElement.prototype.toJSON = () => "{}"; |
Beta Was this translation helpful? Give feedback.
-
|
To avoid browser lag when putting DOM references into a Zustand store, you can utilize the Here's an example of how you can configure your store to use the {
name: "bear",
stateSanitizer: (state) => {
return JSON.parse(
JSON.stringify(state, (_key, value) => {
if (typeof value === "function") return undefined;
if (value instanceof HTMLElement)
return `${value.tagName} ${value.className}`;
if (value instanceof Date) return value.toLocaleString();
return value;
})
);
},
}This configuration helps sanitize the state, preventing potential performance issues caused by DOM references in Chrome and Edge browsers. Additionally, you can set Hope this helps! If it solves your issue, could you please mark this comment as the answer? It helps others find the solution faster. 🙏 |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Hello
I tried putting the dom reference into the store to make it easier for me to use, but I found that it made the browser lag.
I tested in three browsers
chrome:117
edge:117
firefox:116
Chrome and edge are experiencing serious lags
firefox runs fine
Is there something wrong with my operation or should I not put the dom reference into the store?
Link to reproduction
https://codesandbox.io/s/zustand-dom-test-d7g4l3?file=/src/App.js
You can see the difference by changing the comment on this line
Beta Was this translation helpful? Give feedback.
All reactions