ReactMedium

When to use which state tool?

Pick the tool by the kind of state: local UI state in useState/useReducer, values shared by a subtree in Context, shareable view state in the URL, server data in TanStack Query/SWR/RTK Query or framework loaders and Server Components, form state in a form library, and app-wide client state in Zustand, Jotai or Redux Toolkit. Most apps combine several.

Decision guide

StateTool
Input text, toggles, open menususeState
Complex local transitionsuseReducer
Theme, locale, current userContext (or the auth library's hook)
Filters, search, pagination, selected tabURL search params (React Router useSearchParams, Next.js useSearchParams, nuqs)
API data, caching, refetchingTanStack Query, SWR, RTK Query, route loaders, Server Components
Forms with validationReact Hook Form (+ Zod), TanStack Form, or React 19 form Actions
Cross-cutting client state (cart, editor, wizard)Zustand, Jotai, or Redux Toolkit
Large team, strict conventions, heavy middlewareRedux Toolkit
Fine-grained derived state graphsJotai
Real-time collaborative stateLiveblocks, Yjs, or a backend-synced store

Principles

  1. Start local, lift only when needed.
  2. Separate server state from client state. Do not copy API responses into a global store you then keep in sync by hand.
  3. Prefer the URL for anything users may bookmark or share.
  4. Avoid adding a global store "just in case."
  5. Avoid Recoil (archived since 2025).

Interview answer structure

Classify the state, pick the simplest tool per category, and mention trade-offs (re-render behavior, DevTools, boilerplate, bundle size).

Code example

tsx
1// One page using several kinds of state, each in the right place
2import { useState } from "react";
3import { useSearchParams } from "react-router";
4import { useQuery } from "@tanstack/react-query";
5import { create } from "zustand";
6
7const useCart = create<{ ids: string[]; add: (id: string) => void }>((set) => ({
8 ids: [],
9 add: (id) => set((s) => ({ ids: [...s.ids, id] })), // client app state
10}));
11
12export function ProductsPage() {
13 const [params, setParams] = useSearchParams(); // URL state
14 const category = params.get("category") ?? "all";
15 const [showFilters, setShowFilters] = useState(false); // local UI state
16 const add = useCart((s) => s.add);
17
18 const { data = [], isPending } = useQuery({ // server state
19 queryKey: ["products", category],
20 queryFn: () => fetch(`/api/products?category=${category}`).then((r) => r.json()),
21 });
22
23 return (
24 <div>
25 <button onClick={() => setShowFilters((s) => !s)}>Filters</button>
26 {showFilters && (
27 <select value={category} onChange={(e) => setParams({ category: e.target.value })}>
28 <option value="all">All</option><option value="books">Books</option>
29 </select>
30 )}
31 {isPending ? <p>Loading…</p> : data.map((p: { id: string; name: string }) => (
32 <p key={p.id}>{p.name} <button onClick={() => add(p.id)}>Add</button></p>
33 ))}
34 </div>
35 );
36}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app