pmndrs / zustand

🐻 Bear necessities for state management in React

Home Page:https://zustand-demo.pmnd.rs/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Setting nested state make the other states change (their values didn't change)

khuongtp opened this issue · comments

Setting nested state make the other states change (their values didn't change)

Link to reproduction

https://codesandbox.io/p/sandbox/amazing-rhodes-6yp8c5?file=%2Fsrc%2FApp.tsx

Please view the console, you can see that the components that watch count re-render even though count is the same. Refresh the page in code sandbox make it work as expected but on my real machine it doesn't.
Here is my console log no matter how many times I reload the page
image
(I'm using vite btw)
I did some research and found out that this piece of code was the problem, look at count in the useLayoutEffect dependencies, but I still think it's a bug cause the watched state literally didn't change

const count = useTestStore((state) => {
  return state.count;
});
const setNest1 = useTestStore((state) => {
  return state.setNest1;
});

useLayoutEffect(() => {
  setNest1({ key: "nest2", value: { value1: 1 } });
}, [setNest1, count]);

Check List

Please do not ask questions in issues.

  • [ x] I've already opened a discussion before opening this issue, or already discussed in other media.

Please include a minimal reproduction.

Please check this if you're filing an issue regarding TypeScript.

  • [ x] I've read the typescript guide, in particular that create is to be used as create<T>()(...) and not create<T>(...).