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.
Teaches React 19's useOptimistic hook for instant UI feedback during async operations. Use when implementing optimistic patterns like likes, comments, or todos that need immediate visual updates before server confirmation.
/plugin marketplace add djankies/claude-configs/plugin install react-19@claude-configsThis skill is limited to using the following tools:
startTransition for async operations
</overview>
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>
);
}
</workflow>
<examples>
## 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.
</examples>
If handling Prisma transaction errors in optimistic updates, use the handling-transaction-errors skill from prisma-6 for graceful P-code error handling. </related-skills>