From harness-claude
Separates server state managed by TanStack Query from client state in Zustand or Jotai, with optimistic updates, caching, and query invalidation for API data plus UI state apps.
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeThis skill uses the workspace's default tool permissions.
> Separate server state from client state and synchronize them with TanStack Query and local stores
Provides React Query (TanStack Query v5) and Zustand v5 patterns for data fetching, caching, mutations, optimistic updates, and client-side state management. Use for useQuery, useMutation, Zustand stores, or caching tasks.
Implements React state management patterns with Redux Toolkit, Zustand, Jotai, React Query for global state, server state, optimistic updates, and library selection.
Guides React state management with useState, useReducer, Context, Zustand, Jotai, TanStack Query, SWR. Covers store setup, optimization, server caching, optimistic updates, normalization.
Share bugs, ideas, or general feedback.
Separate server state from client state and synchronize them with TanStack Query and local stores
useQuery for read operations. The query key uniquely identifies the data.useMutation for write operations. Invalidate related queries on success.queryClient.setQueryData for optimistic updates. Roll back with onError.// hooks/use-todos.ts — server state via TanStack Query
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
interface Todo {
id: string;
title: string;
completed: boolean;
}
export function useTodos() {
return useQuery({
queryKey: ['todos'],
queryFn: async (): Promise<Todo[]> => {
const res = await fetch('/api/todos');
return res.json();
},
staleTime: 5 * 60 * 1000, // Consider fresh for 5 minutes
});
}
export function useToggleTodo() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async ({ id, completed }: { id: string; completed: boolean }) => {
const res = await fetch(`/api/todos/${id}`, {
method: 'PATCH',
body: JSON.stringify({ completed }),
});
return res.json();
},
// Optimistic update
onMutate: async ({ id, completed }) => {
await queryClient.cancelQueries({ queryKey: ['todos'] });
const previous = queryClient.getQueryData<Todo[]>(['todos']);
queryClient.setQueryData<Todo[]>(['todos'], (old) =>
old?.map((t) => (t.id === id ? { ...t, completed } : t))
);
return { previous };
},
onError: (err, vars, context) => {
queryClient.setQueryData(['todos'], context?.previous);
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: ['todos'] });
},
});
}
// stores/ui-store.ts — client state via Zustand
import { create } from 'zustand';
interface UIStore {
searchTerm: string;
filterCompleted: boolean | null;
setSearchTerm: (term: string) => void;
setFilterCompleted: (filter: boolean | null) => void;
}
export const useUIStore = create<UIStore>((set) => ({
searchTerm: '',
filterCompleted: null,
setSearchTerm: (searchTerm) => set({ searchTerm }),
setFilterCompleted: (filterCompleted) => set({ filterCompleted }),
}));
// Component — composing server + client state
function TodoList() {
const { data: todos = [], isLoading } = useTodos();
const searchTerm = useUIStore((s) => s.searchTerm);
const filterCompleted = useUIStore((s) => s.filterCompleted);
const filtered = useMemo(() => {
let result = todos;
if (searchTerm) result = result.filter((t) => t.title.includes(searchTerm));
if (filterCompleted !== null) result = result.filter((t) => t.completed === filterCompleted);
return result;
}, [todos, searchTerm, filterCompleted]);
if (isLoading) return <Spinner />;
return filtered.map((todo) => <TodoItem key={todo.id} todo={todo} />);
}
The separation principle: Server state is async, cached, shared, and can become stale. Client state is synchronous, local, and always fresh. Mixing them (copying API data into Redux) creates synchronization bugs. Let TanStack Query own server state; let your client store own UI state.
When you need both together: Compose in the component layer. The component reads from both sources and combines them. This keeps each system simple and avoids double-caching.
Query invalidation strategy:
invalidateQueries({ queryKey: ['todos'] }) — mark as stale, refetch if mountedqueryClient.setQueryData — update cache directly (optimistic updates)refetchQueries — force immediate refetch regardless of stalenessBackground refetching: TanStack Query refetches stale data on window focus, on reconnect, and on interval (configurable). This keeps server state fresh without manual polling.
Anti-patterns:
useEffect + useState for data fetching (TanStack Query replaces this)https://tanstack.com/query/latest/docs/framework/react/overview