Zustand store factory for a best-in-class developer experience.
Installations
npm install @udecode/zustood
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
16.20.1
NPM Version
8.19.4
Releases
zustand-x@5.0.0
Published on 05 Jan 2025
zustand-x@3.0.4
Published on 05 Jul 2024
zustand-x@3.0.3
Published on 18 Apr 2024
zustand-x@3.0.2
Published on 29 Dec 2023
zustand-x@3.0.1
Published on 10 Dec 2023
zustand-x@3.0.0
Published on 08 Dec 2023
Contributors
Unable to fetch Contributors
Languages
TypeScript (59.89%)
JavaScript (40.11%)
Developer
Download Statistics
Total Downloads
8,876,526
Last Day
2,711
Last Week
12,360
Last Month
110,039
Last Year
3,347,118
GitHub Statistics
370 Stars
201 Commits
25 Forks
11 Watching
2 Branches
7 Contributors
Package Meta Information
Latest Version
2.0.0
Package Id
@udecode/zustood@2.0.0
Unpacked Size
844.82 kB
Size
110.56 kB
File Count
44
NPM Version
8.19.4
Node Version
16.20.1
Publised On
08 Jul 2023
Total Downloads
Cumulative downloads
Total Downloads
8,876,526
Last day
-4.3%
2,711
Compared to previous day
Last week
-47.1%
12,360
Compared to previous week
Last month
10.2%
110,039
Compared to previous month
Last year
0.4%
3,347,118
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Peer Dependencies
1
Zustood
Zustand is a small, fast and scalable state-management solution battle-tested against common pitfalls, like the dreaded zombie child problem, react concurrency, and context loss between mixed renderers. It may be the one state-manager in the React space that gets all of these right.
As zustand is un-opinionated by design, it's challenging to find out the best patterns to use when creating stores, often leading to boilerplate code.
Zustood, built on top of zustand, is providing a powerful store factory which solves these challenges, so you can focus on your app.
1yarn add zustand @udecode/zustood
Visit zustood.udecode.io for the API.
Why zustood over zustand?
- Much less boilerplate
- Modular state management:
- Derived selectors
- Derived actions
immer
,devtools
andpersist
middlewares- Full typescript support
Create a store
1import { createStore } from '@udecode/zustood' 2 3const repoStore = createStore('repo')({ 4 name: 'zustood', 5 stars: 0, 6})
- the parameter of the first function is the name of the store, this is helpful when you have multiple stores
- the parameter of the second function is the initial state of your store
- the main difference with zustand is that you don't need to define a getter and a setter for each field, these are generated by zustood
Note that the zustand store is accessible through:
1// hook store 2repoStore.useStore 3 4// vanilla store 5repoStore.store
Selectors
Hooks
Use the hooks in React components, no providers needed. Select your
state and the component will re-render on changes. Use the use
method:
1repoStore.use.name() 2repoStore.use.stars()
We recommend using the global hooks (see below) to support ESLint hook linting.
Getters
Don't overuse hooks. If you don't need to subscribe to the state, use
instead the get
method:
1repoStore.get.name() 2repoStore.get.stars()
You can also get the whole state:
1repoStore.get.state()
Extend selectors
You generally want to write derived selectors (those depending on other selectors) for reusability. Zustood supports extending selectors with full typescript support:
1const repoStore = createStore('repo')({ 2 name: 'zustood ', 3 stars: 0, 4}) 5 .extendSelectors((set, get, api) => ({ 6 validName: () => get.name().trim(), 7 // other selectors 8 })) 9 .extendSelectors((set, get, api) => ({ 10 // get.validName is accessible 11 title: (prefix: string) => 12 `${prefix + get.validName()} with ${get.stars()} stars`, 13 })) 14 // extend again...
Actions
Update your store from anywhere by using the set
method:
1repoStore.set.name('new name') 2repoStore.set.stars(repoStore.get.stars + 1)
Extend actions
You can update the whole state from your app:
1store.set.state((draft) => { 2 draft.name = 'test'; 3 draft.stars = 1; 4});
However, you generally want to create derived actions for reusability. Zustood supports extending actions with full typescript support:
1const repoStore = createStore('repo')({ 2 name: 'zustood', 3 stars: 0, 4}) 5 .extendActions((set, get, api) => ({ 6 validName: (name: string) => { 7 set.name(name.trim()); 8 }, 9 // other actions 10 })) 11 .extendActions((set, get, api) => ({ 12 reset: (name: string) => { 13 // set.validName is accessible 14 set.validName(name); 15 set.stars(0); 16 }, 17 })) 18 // extend again...
Global store
After having created many stores, it can be difficult to remember which one to import. By combining all the stores, selectors and actions, just pick what you need using TS autocomplete.
1import { mapValuesKey } from '@udecode/zustood'; 2 3// Global store 4export const rootStore = { 5 auth: authStore, 6 combobox: comboboxStore, 7 contextMenu: contextMenuStore, 8 editor: editorStore, 9 modal: modalStore, 10 repo: repoStore, 11 toolbar: toolbarStore, 12}; 13 14// Global hook selectors 15export const useStore = () => mapValuesKey('use', rootStore); 16 17// Global getter selectors 18export const store = mapValuesKey('get', rootStore); 19 20// Global actions 21export const actions = mapValuesKey('set', rootStore);
Global hook selectors
1useStore().repo.name() 2useStore().modal.isOpen()
By using useStore()
, ESLint will correctly lint hook errors.
Global getter selectors
1store.repo.name() 2store.modal.isOpen()
These can be used anywhere.
Global actions
1actions.repo.stars(store.repo.stars + 1) 2actions.modal.open()
These can be used anywhere.
Options
The second parameter of createStore
is for options:
1export interface CreateStoreOptions<T extends State> { 2 middlewares?: any[]; 3 devtools?: DevtoolsOptions; 4 immer?: ImmerOptions; 5 persist?: PersistOptions; 6}
Middlewares
Zustood is using these middlewares:
immer
: required. Autofreeze can be enabled usingimmer.enabledAutoFreeze
option.devtools
: enabled ifdevtools.enabled
option istrue
.persist
: enabled ifpersist.enabled
option istrue
.persist
implementsPersistOptions
interface from zustand- custom middlewares can be added using
middlewares
option
Contributing and project organization
Ideas and discussions
Discussions is the best place for bringing opinions and contributions. Letting us know if we're going in the right or wrong direction is great feedback and will be much appreciated!
Become a Sponsor!
Contributors
🌟 Stars and 📥 Pull requests are welcome! Don't hesitate to share your feedback here. Read our contributing guide to get started.
License
No vulnerabilities found.
No security vulnerabilities found.