implementing-optimistic-updates
Teaches useOptimistic hook for immediate UI updates during async operations in React 19. Use when implementing optimistic UI patterns, instant feedback, or reducing perceived latency.
When & Why to Use This Skill
This Claude skill provides expert guidance on implementing the React 19 `useOptimistic` hook to build high-performance, responsive user interfaces. It helps developers master optimistic UI patterns, allowing applications to provide immediate visual feedback during asynchronous operations, significantly reducing perceived latency and improving overall user experience (UX).
Use Cases
- Social Media Interactions: Implementing instant 'Like' or 'Reaction' updates that reflect immediately while the server request processes in the background.
- Task Management: Adding, deleting, or reordering items in a list (e.g., To-Do apps) with zero-latency feedback to keep the user flow uninterrupted.
- Messaging Systems: Displaying sent messages in a chat interface immediately with a 'sending' indicator until the backend confirms delivery.
- E-commerce Actions: Updating shopping cart quantities or wishlists instantly to provide a snappy, desktop-like feel on web platforms.
- Data Dashboards: Reflecting state changes in complex forms or toggles immediately while ensuring data consistency through background synchronization.
| name | implementing-optimistic-updates |
|---|---|
| description | Teaches useOptimistic hook for immediate UI updates during async operations in React 19. Use when implementing optimistic UI patterns, instant feedback, or reducing perceived latency. |
| allowed-tools | Read, Write, Edit |
| version | 1.0.0 |
Optimistic UI Updates with useOptimistic
This skill teaches you how to use React 19's `useOptimistic` hook for immediate UI feedback during async operations. - User mentions optimistic updates, instant feedback, or perceived performance - Working with mutations that should feel instant (likes, comments, todos) - Need to show pending states before server confirmation `useOptimistic` enables immediate UI updates that revert if the operation fails:- Shows anticipated result immediately
- Reverts to actual state when async completes
- Provides better UX than waiting for server
- Works with
startTransitionfor async operations
import { useOptimistic, startTransition } from 'react';
function MessageList({ messages, sendMessage }) {
const [optimisticMessages, addOptimisticMessage] = useOptimistic(
messages,
(state, newMessage) => [...state, { ...newMessage, sending: true }]
);
const handleSend = async (text) => {
addOptimisticMessage({ id: Date.now(), text });
startTransition(async () => {
await sendMessage(text);
});
};
return (
<ul>
{optimisticMessages.map((msg) => (
<li key={msg.id}>
{msg.text} {msg.sending && <small>(Sending...)</small>}
</li>
))}
</ul>
);
}
## Like Button Example
function LikeButton({ postId, initialLikes }) {
const [optimisticLikes, addOptimisticLike] = useOptimistic(
initialLikes,
(state, amount) => state + amount
);
const handleLike = async () => {
addOptimisticLike(1);
startTransition(async () => {
await fetch(`/api/posts/${postId}/like`, { method: 'POST' });
});
};
return (
<button onClick={handleLike}>
❤️ {optimisticLikes}
</button>
);
}
For comprehensive useOptimistic documentation, see: research/react-19-comprehensive.md lines 182-240.
NEVER
- Mutate state directly in update function
- Use for critical operations that must succeed
- Skip error handling for failed optimistic updates
If handling Prisma transaction errors in optimistic updates, use the handling-transaction-errors skill from prisma-6 for graceful P-code error handling.