Putting dom reference into store will cause lag #2079
-
SummaryHello I tested in three browsers chrome:117 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 reproductionhttps://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.
Replies: 3 comments 2 replies
-
|
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.


It should because of the
devtoolsmiddleware. Remove it or adding thestateSanitizeroption.The root cause should be related to facebook/react#24360
setting this property also work