# Actions & State Mutations

> Update your Zustand store with simple, synchronous, or asynchronous functions.

Subject: State Management | Lesson 5 of 22 | 5 min read
Interactive lesson: https://codewithanimation.in/state-management/zustand-actions
Previous: [Creating a Zustand Store](https://codewithanimation.in/state-management/creating-zustand-store.md) | Next: [Selectors & useShallow](https://codewithanimation.in/state-management/zustand-selectors-and-shallow.md)

## What are Zustand Actions?

In Zustand, 'actions' are simply functions that you define inside your store to modify the state. Because Zustand does not enforce strict reducers or dispatchers like Redux, you can write plain JavaScript functions.

You mutate the store's state using the `set` function provided by `create`.

## Why is this better than Redux reducers?

Traditional Redux requires defining string constants (`ADD_TODO`), action creators, and a massive `switch` statement inside a reducer just to update a single value.

Zustand collapses all of that. You just write a function, call `set`, and you're done. The `set` function automatically performs a shallow merge on the top level of your state object, meaning you don't have to manually copy over unaffected properties.

## Using the `set` function

The `set` function can be used in two ways:

1. **Passing an object** (when you don't care about the previous state): `set({ count: 0 })`

2. **Passing a callback** (when the next state depends on the previous state): `set((state) => ({ count: state.count + 1 }))`

Async actions are trivial. You just make your action function `async`, await your fetch call, and then call `set` when the data arrives. No extra middleware like `redux-thunk` is needed!

## Common mistake: The Nested Object Trap

A common mistake in Zustand is forgetting that `set` only merges the *first* level of the object. If you have deeply nested state (e.g., `user: { profile: { name: 'Alice' } }`), calling `set({ user: { profile: { name: 'Bob' } } })` will overwrite the entire `user` object, destroying any other properties inside it. For deep updates, you either need to spread manually, or use a library like Immer.

## Tip: Keep actions in the store

While it is technically possible to import a Zustand store and mutate its state from anywhere, it is a best practice to define all state-modifying logic *inside* the `create` block. This keeps your store predictable and centralized.

## Try it: Store Mutations in Action

Use the top toolbar to interact with the Zustand store. Notice how clicking the actions triggers the `set` function and updates the state object predictably.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/state-management/zustand-actions

## Check yourself

### Question 1

How do you handle asynchronous data fetching in a Zustand action?

- A. Install the zustand-thunk middleware and wrap your action.
- B. Just use an async function and call set() when the promise resolves.
- C. You can't. Zustand only supports synchronous state updates.

Correct answer: B

Zustand does not care if your action is synchronous or asynchronous. You can simply mark the function as async, await your data, and call set().

- Why not A: Zustand doesn't require any middleware for async actions.
- Why not C: Zustand fully supports async actions out of the box.

## Remember this

- Actions are plain functions defined alongside state in the store.
- The `set` function automatically shallow-merges top-level state.
- Use a callback `set((state) => ...)` when relying on previous state.
- Async actions work out-of-the-box with standard async/await.
