Skip to content
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

fix: updating the array using push in zustand does not actually trigger component updates #1973

Merged
merged 3 commits into from
Jun 15, 2023

Conversation

Jiacheng787
Copy link
Contributor

As stated in the documentation, all states in Zustand are immutable. Therefore, when updating arrays or nested objects, it is recommended to use immutable methods. Mutable methods like Array.prototype.push() may not be detected by Zustand.

Although mutable methods may still work in practice, issues may arise when using performance optimization techniques such as useCallback and memo. This suggests that the component render actually caused by parent components, rather than Zustand.

While I am not highly experienced in using Zustand, I personally prefer the immutable approach. If you prefer the mutable approach (as seen in Redux Toolkit), integrating Immer into Zustand may be a viable option.

https://docs.pmnd.rs/zustand/guides/updating-state#normal-approach
https://docs.pmnd.rs/zustand/integrations/immer-middleware

@Yidadaa
Copy link
Collaborator

Yidadaa commented Jun 15, 2023

Delete all commented code, and then i will approve this pr.

@Jiacheng787
Copy link
Contributor Author

Delete all commented code, and then i will approve this pr.

Done.

@Yidadaa Yidadaa merged commit 9d1a848 into ChatGPTNextWeb:main Jun 15, 2023
1 of 2 checks passed
alchemist139 pushed a commit to alchemist139/ChatGPT-Next-Web that referenced this pull request Sep 21, 2023
fix: updating the array using push in zustand does not actually trigger component updates
gaogao1030 pushed a commit to gaogao1030/ChatGPT-Next-Web that referenced this pull request May 16, 2024
fix: updating the array using push in zustand does not actually trigger component updates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants