On this page

02. What are Different Types of State in React?

📄 Noteseasy#state#server-state#url-state#architecture

Short answer

State in a React app falls into a few categories: local UI state (owned by one component), shared/lifted state (owned by a common parent), global client state (auth, theme, cart), server state (cached API data), URL state (route params, search params) and form state. Each has a natural home and a best-fit tool, and most bugs come from putting state in the wrong category.

Explanation

Classifying state is the first step to choosing a tool. A useful analogy is a house: your pocket (local), the family fridge (shared), the house address everyone knows (global), the supermarket you shop from (server) and the street sign outside (URL).

TypeExampleTypical tool
Local UI stateModal open, input text, active tabuseState, useReducer
Shared (lifted) stateFilter used by a list and its toolbarLift to common parent, pass props
Global client stateLogged-in user, theme, cart, feature flagsContext, Redux Toolkit, Zustand
Server stateProducts, orders, user profile from APIRTK Query, TanStack Query, SWR, Next.js server components
URL statePage number, search query, selected idReact Router useSearchParams, Next.js searchParams
Form stateField values, errors, touched, dirtyReact Hook Form, Formik, React 19 form actions
Derived stateFiltered list, totals, isValidCompute in render, useMemo, selectors
Persistent stateRemembered preferences, draftslocalStorage, IndexedDB, cookies

Why server state is different

Server state is not really yours. It is a cached copy of data that lives on a server and can go stale. It needs caching, deduplication, refetching, invalidation and loading/error handling. Writing all of that by hand in Redux or useEffect is the classic mistake; libraries like RTK Query and TanStack Query exist exactly for this.

Why URL state matters

If the user should be able to refresh, bookmark or share a view (filters, pagination, selected tab), the URL is the source of truth. Copying it into useState creates two sources that drift apart.

Derived state is not state

If a value can be calculated from other state or props, compute it. Storing it creates sync bugs.

Example

import { useState, useMemo } from "react";
import { useSearchParams } from "react-router-dom";
import { useQuery } from "@tanstack/react-query";

export function ProductPage() {
  const [isFilterOpen, setFilterOpen] = useState(false);       // local UI
  const [searchParams, setSearchParams] = useSearchParams();   // URL state
  const category = searchParams.get("category") ?? "all";

  const { data: products = [], isLoading } = useQuery({         // server state
    queryKey: ["products"],
    queryFn: () => fetch("/api/products").then((r) => r.json()),
  });

  const visible = useMemo(                                     // derived
    () => products.filter((p) => category === "all" || p.category === category),
    [products, category]
  );

  if (isLoading) return <p>Loading...</p>;
  return (
    <>
      <button onClick={() => setFilterOpen((o) => !o)}>Filters</button>
      {isFilterOpen && (
        <select value={category} onChange={(e) => setSearchParams({ category: e.target.value })}>
          <option value="all">All</option>
          <option value="shoes">Shoes</option>
        </select>
      )}
      <ul>{visible.map((p) => <li key={p.id}>{p.name}</li>)}</ul>
    </>
  );
}

Common mistakes

  • Putting API responses in Redux slices with hand-written loading flags instead of using a server-state cache.
  • Duplicating URL params into useState and getting out of sync on back/forward navigation.
  • Making everything global "just in case", which causes needless re-renders and coupling.
  • Storing derived values in state.

Follow-up questions

  • How is server state different from client state? Server state is owned remotely, can be stale and is shared with other users; client state is owned by the browser session and always current.
  • Where would you keep a selected tab? Local state if it is cosmetic; URL state if users should be able to link to it.
  • Is Context a state manager? No, it is a dependency-injection mechanism; the state still lives in useState/useReducer above the provider.

Quick recap

  • Classify state first: local, shared, global, server, URL, form, derived.
  • Server state needs a cache library, not a hand-rolled store.
  • URL is the source of truth for shareable views.
  • Derived values should be computed, not stored.