04 / 09
Actions as events, immer and resetting the store.
Actions are the functions that change the state, and they never change themselves. TkDodo recommends putting them in a separate actions object inside the store. That way you expose all of them through a single hook:1
type CartState = {
items: CartItem[];
actions: {
addProduct: (product: Omit<CartItem, "quantity">) => void;
removeProduct: (id: number) => void;
};
};
const useCartStore = create<CartState>()((set) => ({
items: [],
actions: {
addProduct: (product) =>
set((state) => ({ items: [...state.items, { ...product, quantity: 1 }] })),
removeProduct: (id) =>
set((state) => ({ items: state.items.filter((item) => item.id !== id) })),
},
}));
export const useCartItems = () => useCartStore((state) => state.items);
export const useCartActions = () => useCartStore((state) => state.actions);function RemoveButton({ id }: { id: number }) {
const { removeProduct } = useCartActions();
return <button onClick={() => removeProduct(id)}>Remove</button>;
}This does not contradict the "atomic selector" rule from the Selectors and re-renders lesson. Because the actions object never changes, it behaves like a single atomic value, and you can destructure whatever you want out of it.1
Tip
Because set merges at the top level, the actions object survives updates. It is only removed if you replace the state entirely with set(..., true).
It is easy to name an action something like setItems and write the logic in the component. TkDodo recommends the rule he takes from the Redux style guide: model actions as events.12 The component says what happened and the store decides what to do.
// ❌ The logic is in the component; everyone who uses it repeats the same code
const { setItems } = useCartActions();
setItems(items.some((i) => i.id === product.id) ? items.map(/* ... */) : [...items, product]);
// ✅ The logic is in the store
const { addProduct } = useCartActions();
addProduct(product);addProduct: (product) =>
set((state) => {
const existing = state.items.find((item) => item.id === product.id);
if (existing) {
return {
items: state.items.map((item) =>
item.id === product.id ? { ...item, quantity: item.quantity + 1 } : item,
),
};
}
return { items: [...state.items, { ...product, quantity: 1 }] };
}),The business rule (raise the quantity when the same product is added) sits in one place and is easy to test.
The chain of maps and spreads above gets harder to read as it goes deeper. With Immer you write as though you were changing the state, and Immer produces a new object behind the scenes.3 Zustand provides a middleware for it, and you install the package separately:4
$ pnpm add immerimport { create } from "zustand";
import { immer } from "zustand/middleware/immer";
type CartState = {
items: CartItem[];
addProduct: (product: Omit<CartItem, "quantity">) => void;
};
const useCartStore = create<CartState>()(
immer((set) => ({
items: [],
addProduct: (product) =>
set((state) => {
const existing = state.items.find((item) => item.id === product.id);
if (existing) existing.quantity += 1;
else state.items.push({ ...product, quantity: 1 });
}),
})),
);The function you give set no longer returns anything; it changes the draft. That only holds inside the immer middleware. Without the middleware, state.items.push breaks the store and the screen does not update.3
When to use it? If the state is nested a few levels deep, immer shortens the code noticeably. For flat state a spread is enough; do not add a dependency you do not need.
When somebody signs out, or an order is completed, the store has to go back to its initial state. The third parameter of the function you pass to create is the store itself, and getInitialState() gives you the first state:5
const useCartStore = create<CartState>()((set, _get, store) => ({
items: [],
couponCode: null,
actions: {
// ...
checkoutCompleted: () => set(store.getInitialState()),
},
}));You do not have to copy the initial values into a separate constant.
The documentation states the recommended route plainly: keep the actions in the store with the state. As an alternative, you can also write the actions at module level with setState:67
export const useFilterStore = create<FilterState>()(() => ({
category: "all",
search: "",
}));
export const setCategory = (category: string) => useFilterStore.setState({ category });
export const resetFilters = () => useFilterStore.setState(useFilterStore.getInitialState());The advantage: calling an action needs no hook, and it can be imported from any file. The documentation says this route has no disadvantage, while keeping them together provides encapsulation.7 Pick one in a project and stay consistent.
Write addProduct first in the component with setItems and then in the store as an event. If you have to add the same product from two different components, which is less code?
Add checkoutCompleted to the cart and reset it with getInitialState().
Remove the immer middleware but leave the state.items.push line. Does the screen update?
Article: TkDodo, "Working with Zustand"; separating the actions and modelling them as events.1
Official docs: "Flux inspired practice"; updating with set, keeping the actions together, and the reducer pattern.6
Official docs: "immer"; using the middleware and Immer's rules.4
Sources
: — November 20, 2022. Gathering the actions in a separate object, and modelling them as events.
Naming an action as an event, and keeping the logic in the store.
Copying a nested object by hand, and shortening it with Immer.
The `immer` middleware, the installation requirement and its use.
Resetting with `store.getInitialState()`.
The recommended patterns: updating with `set` and keeping the actions together.
Writing the actions at module level with `setState`.
Check the lesson's main ideas with three questions.
Your progress is stored only in this browser.
Last updated: November 3, 2026