Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

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

Closed
khuongtp opened this issue Apr 24, 2024 · 0 comments
Closed

Comments

@khuongtp
Copy link

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>(...).
@pmndrs pmndrs locked and limited conversation to collaborators Apr 24, 2024
@dai-shi dai-shi converted this issue into discussion #2506 Apr 24, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant