From harness-claude
Implements optimistic updates in TanStack Query: snapshot cache before mutations, apply immediate UI changes, rollback on errors, invalidate on settle. For responsive toggles, likes, reorders despite latency.
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeThis skill uses the workspace's default tool permissions.
> Update the UI immediately on mutation and roll back automatically if the server request fails
Apply optimistic and pessimistic cache updates using onQueryStarted in RTK Query mutations for instant UI feedback and automatic rollback on failure.
Provides expertise in TanStack Query for React/Next.js data fetching, caching (staleTime/gcTime), mutations, optimistic updates, cache invalidation, and App Router SSR hydration.
Implements TanStack Query v5 in React apps for API data fetching, server state caching, mutations, optimistic updates, infinite scroll, streaming AI responses, and tRPC v11 integration.
Share bugs, ideas, or general feedback.
Update the UI immediately on mutation and roll back automatically if the server request fails
onMutate callback to snapshot the current cache value and apply the optimistic update before the request fires.onMutate with queryClient.cancelQueries() to prevent race conditions.onMutate — TanStack Query passes it to onError as context.onError, use the context (the snapshot) to restore the cache to its pre-mutation state with queryClient.setQueryData().onSettled, call queryClient.invalidateQueries() to sync the cache with the actual server state regardless of success or failure.queryClient.setQueryData() with an updater function (not a value) to apply the optimistic change atomically.// mutations/toggle-todo.ts
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { todoKeys } from '@/queries/todos';
interface Todo {
id: string;
completed: boolean;
title: string;
}
export function useToggleTodo() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ id, completed }: { id: string; completed: boolean }) =>
fetch(`/api/todos/${id}`, {
method: 'PATCH',
body: JSON.stringify({ completed }),
}).then((r) => r.json()),
onMutate: async ({ id, completed }) => {
// 1. Cancel in-flight refetches to avoid overwriting optimistic update
await queryClient.cancelQueries({ queryKey: todoKeys.lists() });
// 2. Snapshot the current value
const previousTodos = queryClient.getQueryData<Todo[]>(todoKeys.list({}));
// 3. Apply optimistic update
queryClient.setQueryData<Todo[]>(
todoKeys.list({}),
(old) => old?.map((todo) => (todo.id === id ? { ...todo, completed } : todo)) ?? []
);
// 4. Return snapshot as context for rollback
return { previousTodos };
},
onError: (_error, _variables, context) => {
// Roll back to snapshot
if (context?.previousTodos) {
queryClient.setQueryData(todoKeys.list({}), context.previousTodos);
}
},
onSettled: () => {
// Sync with server regardless of success/failure
queryClient.invalidateQueries({ queryKey: todoKeys.lists() });
},
});
}
Optimistic updates are a UX pattern where the UI assumes a mutation will succeed and updates immediately, then corrects itself if the server disagrees. The key implementation detail is maintaining the ability to roll back.
Race condition prevention: Without cancelQueries, an in-flight background refetch could overwrite your optimistic update with stale server data while the mutation is in-flight. Cancelling in-flight queries for the affected key prevents this.
onMutate return value: Whatever onMutate returns becomes the context parameter in onError and onSettled. This is the standard channel for passing the snapshot to the rollback handler.
onSettled for final sync: Always call invalidateQueries in onSettled (not just onSuccess). Even on success, the server may have applied additional business logic that the optimistic update did not account for. onSettled fires on both success and error, ensuring the cache always reflects reality after the operation completes.
Updater functions: queryClient.setQueryData(key, updater) where updater is a function receives the current cached value and returns the new value. This is atomic — use it instead of reading and writing separately to avoid stale closure issues.
When NOT to use: For mutations where the user should wait for server confirmation before seeing UI changes (payments, irreversible actions), use pessimistic updates instead — update the cache only in onSuccess after the server confirms.
https://tanstack.com/query/latest/docs/framework/react/guides/optimistic-updates