React useOptimistic Hook: Building Instant UI Updates in 2026
React useOptimistic Hook: Building Instant UI Updates in 2026
import { useOptimistic } from 'react';
function TodoList({ todos, addTodo }) {
const [optimisticTodos, addOptimisticTodo] = useOptimistic(
todos,
(state, newTodo) => [...state, { ...newTodo, pending: true }]
);
async function handleAdd(text) {
addOptimisticTodo({ text, id: Date.now() });
await addTodo(text);
}
return (
<ul>
{optimisticTodos.map(todo => (
<li key={todo.id} style={{ opacity: todo.pending ? 0.5 : 1 }}>
{todo.text}
</li>
))}
</ul>
);
}React 19 introduced the useOptimistic hook that lets you show an optimistic state while an async action is in progress. This creates a snappier user experience by updating the UI immediately before the server confirms the change.
How it works:
import { useOptimistic } from 'react';
function TodoList({ todos, addTodo }) {
const [optimisticTodos, addOptimisticTodo] = useOptimistic(
todos,
(state, newTodo) => [...state, { ...newTodo, pending: true }]
);
async function handleAdd(text) {
addOptimisticTodo({ text, id: Date.now() });
await addTodo(text);
}
return (
<ul>
{optimisticTodos.map(todo => (
<li key={todo.id} style={{ opacity: todo.pending ? 0.5 : 1 }}>
{todo.text}
</li>
))}
</ul>
);
}