# Client vs. Server State

> Why putting your API data into your global UI store is an anti-pattern, and how modern React separates the two.

Subject: React | Lesson 26 of 48 | 4 min read
Interactive lesson: https://codewithanimation.in/react/client-vs-server-state
Previous: [Portals](https://codewithanimation.in/react/portals.md) | Next: [Data Fetching & Caching](https://codewithanimation.in/react/data-fetching-caching.md)

## The Two Types of State

**Client State (UI State):** Data that your React app completely owns. It is synchronous, temporary, and lost when you refresh the page. Examples: Is this dropdown open? Is dark mode on? What did the user type into this uncontrolled input?

**Server State (Remote Data):** Data that fundamentally belongs to a database on a server. It is asynchronous, persistent, and shared among many users. Examples: A list of products, a user's profile details.

## The Redux Mistake

For years, React developers put *everything* into global state managers like Redux.

But putting Server State into Client State is a nightmare. Because it's asynchronous, you suddenly have to manage `isLoading`, `isError`, and `data` manually for every single API call. Worse, the moment you put the data in Redux, it is 'stale' (out of date), because someone else might have updated the database.

You end up writing hundreds of lines of code just to handle caching and background updates.

## The Modern Split

Modern React architecture draws a hard line between these two:

1. **Use a UI Store (Zustand, Context) ONLY for Client State.** (e.g., `setDarkMode(true)`)

2. **Use a Server Cache (React Query, SWR, Apollo) for Server State.** These tools aren't 'state managers'—they are caching layers that automatically handle loading states, error retries, background fetching, and cache invalidation.

## Common mistake: The Synchronization Problem

If you fetch a list of users and put them in `useState` or Redux, what happens when a new user signs up on the server?

Your app doesn't know. It's looking at a frozen snapshot in client memory. To fix this, you have to write manual code to re-fetch and update the state. A tool like React Query handles this synchronization automatically by re-fetching when the user focuses the window or reconnects to the internet.

## Tip: Do you even need global state?

Once you remove all the API data (Server State) from your global store, you'll often find you barely have any global Client State left! Many modern React apps don't use Redux or Zustand at all—they just use React Query for data and local `useState` for UI.

## Try it: The Great Divide

Compare the characteristics of Client State vs Server State. Notice how different the problems are that they need to solve.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/client-vs-server-state

## Check yourself

### Question 1

Which of the following is the best example of Client State?

- A. The current list of comments on a blog post.
- B. Whether the 'Settings' modal is currently open or closed.
- C. The current price of a stock.

Correct answer: B

UI State (Client State) is ephemeral data that exists only in the browser's memory to track the current visual interface.

- Why not A: Comments are stored in a database; this is Server State.
- Why not C: This data comes from an external API; this is Server State.

### Question 2

Why is it an anti-pattern to store API data in a standard state manager like `useState` or Redux?

- A. Because React forbids arrays of objects in state.
- B. Because API data is synchronous.
- C. Because it forces you to manually manage caching, staleness, loading flags, and background synchronization.

Correct answer: C

Standard state managers don't know how to talk to a network. They assume they own the data, which makes keeping it synchronized with a server extremely difficult.

- Why not A: React fully supports complex data structures in state.
- Why not B: API data is fundamentally asynchronous.

### Question 3

What is the modern tool of choice for managing Server State in React?

- A. React Query (or SWR / Apollo)
- B. Zustand
- C. The Context API

Correct answer: A

React Query acts as an asynchronous caching layer, completely automating the complexity of server state.

- Why not B: Zustand is excellent, but it is primarily a Client State manager.
- Why not C: Context is for dependency injection/prop-drilling, not for managing asynchronous server caching.

## Remember this

- Client State is synchronous UI data (owned by the browser).
- Server State is asynchronous remote data (owned by the database).
- Never put Server State into a standard Client State manager (like Redux or `useState`).
- Use a dedicated caching tool like React Query to handle Server State.
- Once you separate them, your global UI state becomes drastically simpler.
