# Global State Management

> How to share data across your entire application without prop drilling or triggering massive re-renders.

Subject: React | Lesson 28 of 48 | 4 min read
Interactive lesson: https://codewithanimation.in/react/global-state-management
Previous: [Data Fetching & Caching](https://codewithanimation.in/react/data-fetching-caching.md) | Next: [Complex State with useReducer](https://codewithanimation.in/react/usereducer.md)

## Escaping Prop Drilling

When two distant components (like a `Sidebar` and a `Header`) need access to the same state (like `isSidebarOpen`), you can't easily pass it as a prop.

Global State Management solves this by moving the state completely *outside* of the React component tree into an independent 'Store'.

Any component, anywhere in the app, can connect to this store to read the state or update it.

## Why not just use Context?

As we learned in 'The Context Performance Trap', React Context is great for dependency injection, but terrible for fast-changing global state because it forces *all* consumers to re-render whenever the context value changes.

Modern global state libraries (like Zustand, Redux, or Jotai) solve this using a pattern called **Selectors**.

## The Power of Selectors

Instead of subscribing to the entire store, a component provides a selector function: `const cartCount = useStore(state => state.cartCount);`

The library uses this selector to 'subscribe' the component ONLY to that specific piece of data.

If the user changes `state.darkMode`, the library checks the `cartCount` selector. Since the cart count didn't change, it prevents the component from re-rendering.

## Common mistake: The Fall of Redux

For years, Redux was the standard for React global state. However, it required immense amounts of boilerplate: Actions, Action Creators, Reducers, Dispatchers, and Providers.

Modern libraries like Zustand have stripped this down. In Zustand, you just define a single hook with your state and update functions, and you can use it anywhere immediately. No Providers, no Reducers, no boilerplate.

## Tip: Don't put everything in global state

A common beginner mistake is putting *local* state (like whether a specific dropdown is open) into the *global* store.

Keep state as local as possible. Only promote it to the global store if multiple unrelated components absolutely need to know about it.

## Try it: Surgical Re-renders

Watch how a modern global store (like Zustand) updates only the specific components that care about the changed data, leaving the rest of the application completely unaffected.

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

## Check yourself

### Question 1

In a modern state library like Zustand, what is the primary purpose of a 'Selector' function (e.g., `state => state.user.name`)?

- A. To deeply clone the state object to prevent mutations.
- B. To subscribe the component to only a specific slice of state, preventing unnecessary re-renders when other state changes.
- C. To combine Client State and Server State together.

Correct answer: B

Selectors are the key to high-performance global state. They allow thousands of components to connect to one massive store without causing app-wide re-renders.

- Why not A: Libraries usually use tools like Immer for immutability, not selectors.
- Why not C: Selectors are just functions that extract a piece of an object.

### Question 2

Why are libraries like Zustand often preferred over the native React Context API for complex, frequently updating state?

- A. Because Context requires you to wrap your app in Providers, which is annoying.
- B. Because Context forces every component consuming the Context to re-render whenever ANY value inside the Context changes.
- C. Because Context cannot store complex objects or arrays.

Correct answer: B

Context has no native mechanism for components to 'bail out' of updates if the specific property they care about hasn't changed.

- Why not A: While true, the performance issues are the primary technical reason.
- Why not C: Context can store any JavaScript value.

### Question 3

When should you move a piece of state from `useState` into a global store?

- A. When you want to keep all your state logic in one file.
- B. When the state needs to be accessed by multiple components that are far apart in the component tree.
- C. When the state involves an array of objects.

Correct answer: B

Global state should be reserved for data that is genuinely 'global' to the application.

- Why not A: Moving state just for file organization is an anti-pattern.
- Why not C: `useState` handles arrays perfectly fine.

## Remember this

- Global state stores live outside the React component tree.
- They solve the Prop Drilling problem without the performance penalties of Context.
- Components use Selectors to subscribe to specific slices of state.
- If unselected state changes, the component does NOT re-render.
- Keep state local by default; only make it global when necessary.
