Q54: How does the useOptimistic hook simplify optimistic UI updates?
useOptimistic is a React hook that allows showing an optimistic state immediately while an asynchronous action (such as a Server Action) is pending. It accepts the current state and an update function, returning the optimistic state alongside a dispatch function. Once the background action completes or errors out, React automatically resets to the true state.
Key Takeaways
Provides instant UI feedback during pending asynchronous actions.
Automatically reverts to true server state once the underlying promise resolves or rejects.
Knowledge Check
What happens to the optimistic state returned by useOptimistic when the underlying async action completes?