Loading...
const [optimisticLikes, addLike] = useOptimistic(likes, (n) => n + 1);
function handleClick() {
addLike();
saveLike();
}const [optimisticLikes, addLike] = useOptimistic(likes, (n) => n + 1);
function handleClick() {
startTransition(async () => {
addLike();
const total = await saveLike();
startTransition(() => setLikes(total));
});
}The optimistic update runs inside a transition and the real state is set after the save, so the count never jumps back.
startTransition, not in a plain event handler.The action finished but the real state was never updated. Set the real state from the server result before the action ends.
Use useState for the real value. Use useOptimistic for a temporary value shown only while an action is running.