-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Learn React with TypeScript - Third Edition
By :
In this section, we will learn about Zustand before using it to refactor the app we have been working on to use it.
Zustand is a popular, performant, and scalable state management library for React that is incredibly simple to use.
The state lives in a centralized store, which is created using Zustand’s create function:
const useCountStore = create((set) => ({
count: 0,
inc: () => set((state) => ({ count: state.count + 1 })),
dec: () => set((state) => ({ count: state.count - 1 })),
})); Like React context, a Zustand store can hold functions as well as state values. The preceding example store contains count state with functions to increment and decrement it.
The create function returns a Hook that can be used to access the store. In the preceding example, we called this Hook, useCountStore.
The create function has an optional generic parameter for the type of the store...