practicebasic
Optimistic like
A post with a like counter. On click, the like takes 800ms to save and the UI doesn't respond until it finishes. Implement useOptimistic so the counter increments instantly and reverts if it fails.
Objectives
- 1.Create the optimistic state with useOptimistic from likes
- 2.On click, call addOptimistic(1) before the async request
- 3.Update the real state with setLikes when the request succeeds
- 4.Show a visual indicator when the like is pending
your code
Hint
useOptimistic(state, updateFn) — the updateFn receives the current state and the value you passed to addOptimistic. Return the new provisional state.
Related concepts
Was this helpful?
Sign in to give feedback
