# Optimistic Updates (useOptimistic)

> Instant UI feedback that automatically reverts on failure.

Subject: Next.js | Lesson 35 of 65 | 6 min read
Interactive lesson: https://codewithanimation.in/nextjs/use-optimistic
Previous: [Pending States (useFormStatus)](https://codewithanimation.in/nextjs/use-form-status.md) | Next: [Error Handling (Actions)](https://codewithanimation.in/nextjs/actions-error-handling.md)

## Lying to the User (In a Good Way)

`useOptimistic` is a React Hook that lets you temporarily update your UI with the *expected* result of a mutation before the server has actually confirmed it.

It makes your application feel incredibly fast and responsive, mimicking zero-latency native apps.

## The Speed of Perception

If a user clicks a 'Like' button, they don't want to see a loading spinner for 500 milliseconds while the server updates the database.

They expect the heart icon to fill in instantly. If the network request subsequently fails, the UI should gracefully revert back to its original state.

## State vs. Optimism

You initialize the hook with the true state: `const [optLikes, addOptLike] = useOptimistic(likes, (state, newAmount) => state + newAmount)`.

Inside your event handler, you first call `addOptLike(1)` to instantly update the UI. Then, you trigger your Server Action to actually update the database.

## Common mistake: The Reversion Trap

Historically, developers implemented optimistic updates using standard `useState`. If the server request failed, they had to remember to write a `catch` block that manually subtracted the like back off.

`useOptimistic` eliminates this trap. As soon as the Server Action finishes (successfully or with an error), React automatically discards the optimistic state and reverts to the true state.

## Tip: Transition Dependency

`useOptimistic` only works if the background mutation is wrapped in a React Transition.

Because Server Actions are automatically wrapped in transitions by Next.js, they pair perfectly with this hook without requiring manual `startTransition` wrappers.

## Try it: The Ghost Reversion

Try the 'Like' button with the Server Error toggle OFF. It feels instant. Now, turn the Server Error toggle ON and click 'Like'. The UI instantly updates (optimistically), but watch how it automatically reverts a second later when the server rejects the request.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/use-optimistic

## Check yourself

### Question 1

What happens to the optimistic state if the underlying Server Action throws an error?

- A. React throws an Unhandled Rejection Error and crashes the client.
- B. The optimistic state remains applied until you manually revert it.
- C. React automatically discards the optimistic state and reverts to the true state.
- D. Next.js automatically retries the Server Action three times.

Correct answer: C

The magic of `useOptimistic` is automatic reversion. As soon as the transition completes or fails, the temporary optimistic state is thrown away.

- Why not A: React handles it gracefully.
- Why not B: This is what happened in the old days with useState.
- Why not D: Next.js does not auto-retry mutations.

### Question 2

Why does `useOptimistic` pair so well with Next.js Server Actions?

- A. Because Server Actions are automatically wrapped in React Transitions.
- B. Because Server Actions execute in zero milliseconds.
- C. Because Server Actions bypass the Next.js cache completely.
- D. Because `useOptimistic` can only be imported in Server Components.

Correct answer: A

`useOptimistic` relies on React Transitions to know when to discard the temporary state. Since Next.js automatically wraps Server Actions in transitions, the integration is seamless.

- Why not B: Network requests always take time.
- Why not C: Not necessarily related to optimistic UI.
- Why not D: useOptimistic is a Client Component hook.

## Remember this

- `useOptimistic` instantly updates the UI before a server mutation finishes.
- It prevents the need for loading spinners on micro-interactions (like Upvotes).
- If the server action fails, React automatically reverts the UI to the true state.
- It relies heavily on React Transitions to know when the mutation is complete.
- Next.js Server Actions are automatically wrapped in transitions, making them a perfect pair.
