State Management · Senior · Question 35 of 75
How do you make an optimistic update with SWR's mutate?
- ✓Correct answer: Pass
optimisticDataandrollbackOnErroralong with the async update function - 2Call
mutatetwice: before and after the request - 3SWR doesn't support optimistic updates
Explanation
mutate(updateTodo(newTodo), { optimisticData: [...todos, newTodo], rollbackOnError: true, populateCache: true, revalidate: false }) shows the change immediately and reverts it if the request fails.