How to use zustand in a non-singleton way
See original GitHub issueIn current documentation, store instance is created when the module is executed. All component instances will bind to the same store instance(singleton pattern). Zustand is similar to redux in this way.
But sometimes this is not what I want. Sometimes I want each ComponentA
instance has its own StoreA
instance. In other words, ComponentA instance lifetime === StoreA instance lifetime
. ComponentA
can share its StoreA
instance to its children.
This pattern is very suitable when the data lifetime should be same with component instance lifetime.
Is there a standard way to do this?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top Results From Across the Web
How to use zustand in a non-singleton way · Issue #162 - GitHub
In current documentation, store instance is created when the module is executed. All component instances will bind to the same store ...
Read more >Micro-State management with Zustand in React.
In module state, stores are defined in modules and we export them, they work on the Singleton + Factory design pattern where all...
Read more >Tracking state changes with the Zustand plugin
Zustand is a very fast and minimalistic replacement for state management solutions like Redux. With our Zustand plugin, you can keep track of...
Read more >Zustand - New React State Manager - YouTube
Let's try out the Zustand state manager by building a simple application that grabs a list of Pokemon and filter it with a...
Read more >Live - Zustand - New React State Manager - YouTube
Is Zustand the perfect choice between local component state with prop-drilling and content and a big Redux singleton ? Let's find out!
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
https://github.com/react-spring/jotai is out. Please check it. It’s designed for non-singleton.
If you have question about Jotai, please file an issue there.
We had a discussion: #128 In short, it’s technically possible, at least for now. However, because it’s a bit tricky (can lead to violate rules of hooks, if not done properly), we don’t document it for now as it’s only for power users.
(we are working on a second project for non-singleton use case.)