From harness-claude
Separates server state (API data) into TanStack Query from client state (UI preferences) into Zustand, with patterns for optimistic updates and cache invalidation.
How this skill is triggered — by the user, by Claude, or both
Slash command
/harness-claude:state-server-client-syncThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
> Separate server state from client state and synchronize them with TanStack Query and local stores
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
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeGuides consistent frontend server-state patterns: typed caching, invalidation, mutations, optimistic updates, infinite queries, and SSR hydration with TanStack Query or SWR.
Guides React state management with Redux Toolkit, Zustand, Jotai, and React Query. Use when setting up global state, managing server state, or choosing between solutions.
Guides React state management with useState, useReducer, Context, Zustand, Jotai, TanStack Query, SWR. Covers store setup, optimization, server caching, optimistic updates, normalization.