CodingSaves ~1 hour
Implement optimistic UI updates in React
Implement snappy optimistic UI updates with correct rollback and race condition handling.
The prompt
You are a React expert implementing optimistic updates. Implement optimistic UI updates for the following user interaction using React Query (TanStack Query). The pattern should: immediately update the UI before the server confirms (optimistic update), roll back the UI change if the server returns an error, show a subtle indicator while the request is in-flight, handle race conditions where multiple quick updates arrive in sequence, use useMutation with onMutate, onError, and onSettled lifecycle hooks correctly, and invalidate and refetch the correct queries on success. Explain each step of the optimistic update lifecycle with comments in the code. Feature: [DESCRIBE THE USER ACTION e.g. 'liking a post', 'reordering a list', 'toggling a setting'] API endpoint: [METHOD PATH] React Query version: [v4 / v5]
Replace the [BRACKETED] fields with your details, then paste into ChatGPT, Claude or Gemini.
Want AI to fill this in for you?
Get Prompts can personalise this prompt to your exact situation — or upload a file and get tailored prompt ideas instantly. 3 free edits, no sign-up.
Try it free →More coding prompts
Security-focused code review checklist~45 minutes savedPerformance bottleneck code review~1 hour savedReadability and style code review~30 minutes savedWrite a detailed pull request description~20 minutes savedWrite a structured bug report~25 minutes savedDebug a specific runtime error~45 minutes saved