Sub topic 2
Your first store with create
This chapter assumes you know React function components and hooks, basic TypeScript, object spread, and immutable updates. It builds one small store and explains how create turns it into a hook.
What create returns
create takes a function that builds the initial state and returns a React hook. The state is a plain object that holds both data and the actions that change it. Zustand keeps this object outside React, in a module-level variable, so any component can import the hook and read the same store. No provider is needed.
Install the package first:
npm install zustandA typed counter store
import { create } from 'zustand'
interface CounterState {
count: number
increment: () => void
reset: () => void
}
export const useCounterStore = create<CounterState>()((set) => ({
count: 0,
increment: () => set((state) => ({ count: state.count + 1 })),
reset: () => set({ count: 0 }),
}))The function you pass to create receives set, and it returns the initial state. count is data. increment and reset are actions, which are ordinary functions stored next to the data.
set merges the partial object you give it into the existing state. In reset, only count changes, and the actions stay in place. You can also pass a function. set((state) => ...) receives the current state, so use that form when the new value depends on the old one.
Using the store in components
import { useCounterStore } from './counterStore'
function Count() {
const count = useCounterStore((state) => state.count)
return <p>Count: {count}</p>
}
function Controls() {
const increment = useCounterStore((state) => state.increment)
return <button onClick={increment}>Add one</button>
}The function you pass to the hook is a selector. It picks one value from the state, and the component re-renders only when that value changes. Controls selects an action, which never changes, so it does not re-render when count changes. Selectors get a full chapter later.
Common errors
- Writing
create<T>(...)with one call. In TypeScript, the form iscreate<T>()(...). The extra()lets TypeScript check the type you gave while it infers the rest. Without it, you may get type errors once you add middleware. - Mutating state. Writing
state.count++insidesetchanges the object in place. Zustand compares by reference, so subscribers may not notice. Return a new object instead, or use the immer middleware later in this tutorial. - Expecting
setto replace state.setmerges at the top level only. A nested object you pass replaces the old nested object entirely, so spread it yourself when you update one nested field. - Calling
createinside a component. This builds a new store on every render and loses state. Call it once, at module level.
Exercise
In the store above, reset calls set({ count: 0 }) and does not include increment. Why does increment still work afterwards?
Show answer
set merges the partial object into the existing state at the top level. Only count is overwritten, and increment and reset stay in the state object.