# Actions

> The Actions in React are functions that handle async data mutations inside transitions, managing pending states, errors and optimistic updates.

## What are Actions in React and how do they handle forms?

The functions that run async operations inside a transition. React automatically tracks the pending state, handles errors and optimistic updates. Actions can be passed to the `action` prop of a `<form>` to submit data without manual handlers.

- Topic: Server & Actions
- Type: concept

## Example

Wrong:

```jsx
function Save({ data }) {
  const [loading, setLoading] = useState(false);
  async function onClick() {
    setLoading(true);
    await save(data);
    setLoading(false);
  }
  return <button disabled={loading} onClick={onClick}>Save</button>;
}
```

Right:

```jsx
function Save({ data }) {
  const [isPending, startTransition] = useTransition();
  const onClick = () => startTransition(async () => {
    await save(data);
  });
  return <button disabled={isPending} onClick={onClick}>Save</button>;
}
```

The transition tracks pending state for you, and it resets even when `save()` throws, unlike the manual flag.

## Common mistakes

- Keeping a manual `isLoading` state next to an action, which drifts out of sync with `isPending`.
- Expecting form fields to keep their values after a successful action, though React resets uncontrolled forms.

## Key points

- Mutations run inside `startTransition` or a `<form action>`, not in `onSubmit` with manual `useState` loading flags.
- Pending UI reads `isPending` from `useTransition` or `useActionState`.
- Errors from the action are returned as state or caught by an error boundary, not silently swallowed.

## Interview questions

### What is the difference between an Action and a Server Action?

An Action is any function run inside a transition. A Server Action is an Action that is a Server Function, so its body runs on the server.

### Can I set state after await inside an Action?

Yes, but wrap updates after `await` in another `startTransition`, or they are not marked as part of the transition.

### Do I need useState for errors in an Action?

No, `useActionState` returns whatever the action returns, so an error message can be part of that state.

## Related cards

- [useActionState()](https://wonderium.dev/react/use-action-state.md)
- [useOptimistic()](https://wonderium.dev/react/use-optimistic.md)
- [useFormStatus()](https://wonderium.dev/react/use-form-status.md)
- [useTransition()](https://wonderium.dev/react/use-transition.md)
- [Server Actions](https://wonderium.dev/react/server-actions.md)

---

- Level: [React](https://wonderium.dev/react.md)
- HTML version: https://wonderium.dev/react/actions
- Last updated: 2026-11-25
