Back to ⚡ Performance & Optimization

Implement Optimistic UI Updates

React QueryUXPerformance
---
description: Update UI before server confirms
---

1. **Install React Query**:
// turbo
- Run npm install @tanstack/react-query

2. **Setup Optimistic Mutation**:
const addTodo = useMutation({
mutationFn: (text) => fetch('/api/todos', { method: 'POST', body: JSON.stringify({ text }) }),
onMutate: async (newTodo) => {
await queryClient.cancelQueries({ queryKey: ['todos'] });
const previous = queryClient.getQueryData(['todos']);
queryClient.setQueryData(['todos'], (old) => [...old, newTodo]);
return { previous };
},
onError: (err, newTodo, context) => {
queryClient.setQueryData(['todos'], context.previous);
},
});


3. **Pro Tips**:
- Always implement rollback.
- Show loading states.
By Antigravity Team

How to Use This Workflow

Option A: Project-Specific (Recommended)

  1. Click "Download" above
  2. In your project, create the directory: .agent/workflows/
  3. Save the file as implement-optimistic-ui-updates-react-query.md
  4. In Antigravity, type /implement_optimistic_ui_updates_react_query or just describe what you want to do

Learn more about workflows →

Related Workflows

Recommended Rules

View more rules →

Recommended MCP Servers

View more MCP servers →

Take It Further

Maximize your productivity with these powerful resources

📋

Define Your Standards

Set up coding standards to ensure this workflow produces consistent, high-quality results.

Browse Rules Library
📖

Master Workflows

Learn how to create custom workflows, use Turbo Mode, and build your automation library.

Complete Guide