Understanding module state and immutable state
Zustand is a library that's used to create a store
that holds a state. It's primarily designed for module state, which means you define this store
in a module and export it. It's based on the immutable state model, in which you are not allowed to modify state object properties. Updating states must be done by creating new objects, while unmodified state objects must be reused. The benefit of the immutable state model is that you only need to check state object referential equality to know if there's any update; you don't have to check equality deeply.
The following is a minimal example that can be used to create a count
state. It takes a store
creator function that returns an initial state:
// store.ts import create from "zustand"; export const store = create(() => ({ count: 0 }));
store
exposes some functions such as getState
, setState
, and subscribe
. You can use getState
to get the state...