New package zustand-scoped that tackles nested stores and initializing stores with data #1587
Unanswered
lukesmurray
asked this question in
Show and tell
Replies: 1 comment
-
Nice! I've been looking for a way to nest-stores in a reasonable fashion and will try this out. It would be great if a solution like this was available in zustand itself. Thanks for publishing your package! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I wanted to publicize a package I created, "zustand-scoped" which attempts to tackle two issues I've had with Zustand—combining stores (#291, #161, #163, #178) and initializing stores with data (#82, #552).
The package lets you
create(initialData)
)The package is typesafe and works with almost all zustand middleware (not immer).
How it Works
The package exposes a wrapper around
create
, which makes the return value ofcreate
a "store factory" instead of a hook. You can call the "factory" with initial data to create a store and set the initial data of the store.The package also provides typesafe support for "nested" stores, inspired by this comment (see scoped store docs for details).
Compared with zustand-lens this is much less magical. You need to write your own
set
andget
overrides in the parent store. But as a result "zustand-scoped" supports nesting stores with arbitrary complexity. For instance, you can create an array of nested stores and dynamically add and remove them at runtime.The entire package is also 406B so feel free to add it to your bundle.
Would appreciate any feedback!
Beta Was this translation helpful? Give feedback.
All reactions