How to create multiple instances of a Zustand store?

Viewed 1117

I'm making a map in a component and in this component I'm using Zustand to store the state of each component, the problem is that the store instance is the same for all items, how do I create a new instance for each item?

I've already tried to create a function to do this but it didn't work.

1 Answers

UPDATE:

Zustand doing some updates on their documentation since ver.4 was released. This is in their documentation how to create multiple instance with Slice Pattern https://github.com/pmndrs/zustand/blob/main/docs/guides/typescript.md#slices-pattern

OLD ANSWER:

You can create multiple stories by setup it with different name of store.

Source code: https://codesandbox.io/s/loving-breeze-jshevq

  1. In this example I have created two store (bears and cats) files Create two store files
  2. Initial zustand store for "bears" and "cats" Inital zustand store
  3. Implementation enter image description here

Result:
enter image description here

Related