Make "optimistic" declarative
useOptimistic lets you show the expected outcome of an action before the server confirms. The hook gives you a temporary "pretend" state that reverts automatically if the action fails.
Shape
const [optimistic, addOptimistic] = useOptimistic(realState, reducer). Call addOptimistic with the change, then run the action. The optimistic state holds until the action resolves and the cache revalidates.
Patterns where it pays
- Likes, hearts, favorites — immediate feedback feels essential.
- Toggles where the round-trip is >100ms.
- Adding items to a list (with a sending… indicator).