What is useOptimistic in React 19?

useOptimistic is a powerful hook for creating highly responsive "optimistic UIs". An optimistic UI immediately updates the screen to show what the user expects before the server has actually confirmed the action, making the app feel instantly fast.

How it works:

It takes the current true state from the server and an update function. It returns an "optimistic state" and an addOptimistic function.

When a user takes an action (like "Liking" a post), you call addOptimistic(newValue). React will immediately re-render using the optimistic state. While the actual background network request is happening, the user sees the immediate result. Once the network request finishes and updates the true state, React discards the optimistic state and syncs to the truth.

Error Handling:

If the network request fails, React seamlessly drops the optimistic state and reverts back to the true state, essentially undoing the action automatically.

Code example

tsx
1import React, { useState, useOptimistic } from 'react';
2
3// Simulated server action
4async function likePostAction(postId, currentLikes) {
5 await new Promise(r => setTimeout(r, 1000));
6 // Simulate a random server failure 20% of the time
7 if (Math.random() < 0.2) throw new Error("Network failed");
8 return currentLikes + 1;
9}
10
11export default function SocialPost() {
12 // 1. The True State (from the server)
13 const [trueLikes, setTrueLikes] = useState(10);
14
15 // 2. The Optimistic Hook
16 // It merges the true state with any temporary optimistic updates
17 const [optimisticLikes, addOptimisticLike] = useOptimistic(
18 trueLikes,
19 // The update function: how the state should change optimistically
20 (state, amount) => state + amount
21 );
22
23 // 3. The Action Handler
24 const handleLike = async () => {
25 // A. INSTANTLY update the UI without waiting for the server
26 addOptimisticLike(1);
27
28 try {
29 // B. Actually perform the slow server request
30 const newTrueLikes = await likePostAction('post-123', trueLikes);
31 // C. Update the true state upon success
32 setTrueLikes(newTrueLikes);
33 } catch (error) {
34 // D. If it fails, we don't update the true state.
35 // React will automatically revert 'optimisticLikes' back to 'trueLikes'
36 console.error("Failed to like post");
37 }
38 };
39
40 return (
41 <div className="p-6 border rounded-2xl max-w-sm bg-white shadow-sm">
42 <h3 className="text-xl font-bold mb-2">Just launched my new app! 🚀</h3>
43 <p className="text-slate-500 mb-6">It's incredibly fast thanks to React 19.</p>
44
45 {/* We display the optimistic state so the UI feels instant */}
46 <form action={handleLike}>
47 <button
48 type="submit"
49 className="flex items-center gap-2 px-4 py-2 bg-pink-50 text-pink-600 rounded-full font-bold hover:bg-pink-100 transition-colors"
50 >
51 ❤️ {optimisticLikes} Likes
52 </button>
53 </form>
54 </div>
55 );
56}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React Hooks set.

Open in study app