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
| State | Tool |
|---|---|
| Input text, toggles, open menus | useState |
| Complex local transitions | useReducer |
| Theme, locale, current user | Context (or the auth library's hook) |
| Filters, search, pagination, selected tab | URL search params (React Router useSearchParams, Next.js useSearchParams, nuqs) |
| API data, caching, refetching | TanStack Query, SWR, RTK Query, route loaders, Server Components |
| Forms with validation | React 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 middleware | Redux Toolkit |
| Fine-grained derived state graphs | Jotai |
| Real-time collaborative state | Liveblocks, Yjs, or a backend-synced store |
Principles
- Start local, lift only when needed.
- Separate server state from client state. Do not copy API responses into a global store you then keep in sync by hand.
- Prefer the URL for anything users may bookmark or share.
- Avoid adding a global store "just in case."
- 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 place2import { useState } from "react";3import { useSearchParams } from "react-router";4import { useQuery } from "@tanstack/react-query";5import { create } from "zustand";67const useCart = create<{ ids: string[]; add: (id: string) => void }>((set) => ({8 ids: [],9 add: (id) => set((s) => ({ ids: [...s.ids, id] })), // client app state10}));1112export function ProductsPage() {13 const [params, setParams] = useSearchParams(); // URL state14 const category = params.get("category") ?? "all";15 const [showFilters, setShowFilters] = useState(false); // local UI state16 const add = useCart((s) => s.add);1718 const { data = [], isPending } = useQuery({ // server state19 queryKey: ["products", category],20 queryFn: () => fetch(`/api/products?category=${category}`).then((r) => r.json()),21 });2223 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.