Skip to content
Zustand기술 정리

State management with Zustand

1 min readTech notesMoved from my Tistory blogRead the Korean original

So far, Redux-Toolkit was the only state management I had used. At work, a senior colleague said that project used Zustand for global state management, so I came across Zustand. It was really simple, so I'm going to write down how to use it.

First, install zustand.

bash
npm i zustand

Create the store.

Creating a store also seems much more concise than with Redux-Toolkit.

countStore.ts
Figure 1. countStore.ts
ts
import { create } from "zustand"; // must be {create}; writing it as create causes an error

type countStoreType = {
  // If you use TS, you need to specify the type of useCountStore.
  count: number;
  countUp: () => void;
};

export const useCountStore = create<countStoreType>((set) => ({
  count: 0, // initial value
  countUp: () => {
    // countUp function
    set((state) => ({ ...state, count: state.count + 1 }));
  },
}));

Now any component can manage state with the count value and the countUp function.

jsx
import { useCountStore } from "./store/countStore";

function App() {
  const { count, countUp } = useCountStore(); // need to import the useCountStore declared above
  return (
    <>
      <h1>{count}</h1>
      <button onClick={() => countUp()}>+</button>
    </>
  );
}

export default App;

After using it, I found it really concise, so I could use it easily just by looking at an example.

  • #Zustand
  • #State Management

More in Tech notes