Ce contenu n’est pas encore disponible dans votre langue.
Zustand is a small, fast, and scalable bearbones state management solution. Zustand has a comfy API based on hooks. It isn’t boilerplatey or opinionated, but has enough convention to be explicit and flux-like.
Usage
First Create a Store
Your store is a hook! You can put anything in it: primitives, objects, functions. The set function merges state.
Then Bind your Components, and that’s it!
You can use the hook anywhere, without the need of providers. Select your state and the consuming component will re-render when that state changes.
Comparison
Zustand is one of many state management libraries for React. On this page we will discuss Zustand in comparison to some of these libraries, including Redux, Valtio, Jotai, and Recoil.
Each library has its own strengths and weaknesses, and we will compare key differences and similarities between each.
Zustand vs Redux
State Model
Conceptually, Zustand and Redux are quite similar, both are based on an immutable state model. However, Redux requires your app to be wrapped in context providers; Zustand does not.
When it comes to render optimizations within your app, there are no major differences in approach between Zustand and Redux. In both libraries it is recommended that you manually apply render optimizations by using selectors.
Zustand and Valtio approach state management in a fundamentally different way. Zustand is based on the immutable state model, while Valtio is based on the mutable state model.
The other difference between Zustand and Valtio is Valtio makes render optimizations through property access. However, with Zustand, it is recommended that you manually apply render optimizations by using selectors.
There are two major differences between Zustand and Jotai. Firstly, Zustand is a single store, while Jotai consists of primitive atoms that can be composed together. Secondly, a Zustand store is an external store, making it more suitable when access outside of React is required.
Jotai achieves render optimizations through atom dependency. However, with Zustand it is recommended that you manually apply render optimizations by using selectors.
The difference between Zustand and Recoil is similar to that between Zustand and Jotai. Recoil depends on atom string keys instead of atom object referential identities. Additionally, Recoil needs to wrap your app in a context provider.
Similar to previous optimization comparisons, Recoil makes render optimizations through atom dependency. Whereas with Zustand, it is recommended that you manually apply render optimizations by using selectors.