The count variable inside catch is the old value before increment.
Step 2: Explain why revert fails
Subtracting 1 from stale count does not revert to original because count was already incremented in UI.
Final Answer:
Using stale count value inside catch block -> Option A
Quick Check:
State closure causes revert failure [OK]
Hint: Avoid stale state in async error handlers [OK]
Common Mistakes:
Expecting setState to be async
Ignoring stale closure of state variable
Wrong HTTP method for API call
5. You want to implement optimistic updates for a comment submission in Next.js. Which approach best handles UI update, server confirmation, and error rollback?
hard
A. Reload the page after comment submission to show new comment
B. Add comment to UI state immediately, send API request, remove comment if API fails
C. Add comment to UI state only after API confirms success
D. Send API request first, then add comment to UI state after success
Solution
Step 1: Apply optimistic update pattern
Update UI immediately by adding comment to state to improve responsiveness.
Step 2: Handle server confirmation and errors
Send API request; if it fails, remove the comment from UI to keep data consistent.
Final Answer:
Add comment to UI state immediately, send API request, remove comment if API fails -> Option B
Quick Check:
UI update first, revert on error [OK]
Hint: Add UI item first, remove on failure [OK]
Common Mistakes:
Waiting for API before UI update
Adding comment only after success
Reloading page instead of updating state
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.