Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
@udecode/zustood
Advanced tools
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.
yarn add zustand @udecode/zustood
Visit zustood.udecode.io for the API.
immer
, devtools
and persist
middlewaresimport { createStore } from '@udecode/zustood'
const repoStore = createStore('repo')({
name: 'zustood',
stars: 0,
})
Note that the zustand store is accessible through:
// hook store
repoStore.useStore
// vanilla store
repoStore.store
Use the hooks in React components, no providers needed. Select your
state and the component will re-render on changes. Use the use
method:
repoStore.use.name()
repoStore.use.stars()
We recommend using the global hooks (see below) to support ESLint hook linting.
Don't overuse hooks. If you don't need to subscribe to the state, use
instead the get
method:
repoStore.get.name()
repoStore.get.stars()
You can also get the whole state:
repoStore.get.state()
You generally want to write derived selectors (those depending on other selectors) for reusability. Zustood supports extending selectors with full typescript support:
const repoStore = createStore('repo')({
name: 'zustood ',
stars: 0,
})
.extendSelectors((set, get, api) => ({
validName: () => get.name().trim(),
// other selectors
}))
.extendSelectors((set, get, api) => ({
// get.validName is accessible
title: (prefix: string) =>
`${prefix + get.validName()} with ${get.stars()} stars`,
}))
// extend again...
Update your store from anywhere by using the set
method:
repoStore.set.name('new name')
repoStore.set.stars(repoStore.get.stars + 1)
You can update the whole state from your app:
store.set.state((draft) => {
draft.name = 'test';
draft.stars = 1;
});
However, you generally want to create derived actions for reusability. Zustood supports extending actions with full typescript support:
const repoStore = createStore('repo')({
name: 'zustood',
stars: 0,
})
.extendActions((set, get, api) => ({
validName: (name: string) => {
set.name(name.trim());
},
// other actions
}))
.extendActions((set, get, api) => ({
reset: (name: string) => {
// set.validName is accessible
set.validName(name);
set.stars(0);
},
}))
// extend again...
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.
import { mapValuesKey } from '@udecode/zustood';
// Global store
export const rootStore = {
auth: authStore,
combobox: comboboxStore,
contextMenu: contextMenuStore,
editor: editorStore,
modal: modalStore,
repo: repoStore,
toolbar: toolbarStore,
};
// Global hook selectors
export const useStore = () => mapValuesKey('use', rootStore);
// Global getter selectors
export const store = mapValuesKey('get', rootStore);
// Global actions
export const actions = mapValuesKey('set', rootStore);
useStore().repo.name()
useStore().modal.isOpen()
By using useStore()
, ESLint will correctly lint hook errors.
store.repo.name()
store.modal.isOpen()
These can be used anywhere.
actions.repo.stars(store.repo.stars + 1)
actions.modal.open()
These can be used anywhere.
The second parameter of createStore
is for options:
export interface CreateStoreOptions<T extends State> {
middlewares?: any[];
devtools?: DevtoolsOptions;
immer?: ImmerOptions;
persist?: PersistOptions;
}
Zustood is using these middlewares:
immer
: required. Autofreeze can be enabled using
immer.enabledAutoFreeze
option.devtools
: enabled if devtools.enabled
option is true
.persist
: enabled if persist.enabled
option is true
. persist
implements PersistOptions
interface from
zustandmiddlewares
optionDiscussions 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!
🌟 Stars and 📥 Pull requests are welcome! Don't hesitate to share your feedback here. Read our contributing guide to get started.
FAQs
A modular store factory using zustand
The npm package @udecode/zustood receives a total of 14,421 weekly downloads. As such, @udecode/zustood popularity was classified as popular.
We found that @udecode/zustood demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.