question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use zustand in a non-singleton way

See original GitHub issue

In 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
dai-shicommented, Sep 9, 2020

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.

2reactions
dai-shicommented, Aug 20, 2020

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.)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found