Frontend System Design · Lesson 29 of 86 · 4 min
Global State vs Local Component State
Your answer to "where does this state belong?" reveals your approach to coupling, performance, and long-term maintainability.
This is a fundamental architectural decision. Get it right, and your app is easier to reason about and debug. Get it wrong, and you create a tangled mess of dependencies and performance bottlenecks. In an interview, your reasoning here is more important than naming a specific library.
Default to Local State
Start with the simplest thing that works. Keep state as close as possible to the component that uses it. This is the principle of encapsulation.
Local state is for data that a single component and its direct children care about. If other parts of the app don't need to know, don't tell them. This minimizes your app's "surface area" for bugs and performance issues. A state update confined to one component only re-renders that component and its children.
Use local state for:
- The value of a form input.
- Whether a dropdown or modal is open.
- The loading/error status for a data fetch initiated by one component.
- The current tab index in a tab component.
The trap: Putting ephemeral UI state in a global store. A flag like isSidebarOpen almost never belongs in a global store. If only the App and Sidebar components care about it, manage it in the App component. Prematurely globalizing state adds complexity for no benefit.
When to Go Global
You move state up the tree when multiple components need to share it. You make it global when those components are far apart. "Global" doesn't necessarily mean a Redux store; it can be a React Context at the top of your application tree.
The two main triggers for promoting state to global are:
- Sharing across disconnected components. The user's authentication status needs to be accessed by the main navigation, a profile page, and an admin-only button. These components are scattered across the tree and have no direct parent-child relationship.
- Avoiding deep prop drilling. If you're passing a prop down three or more levels through components that don't use it, you're "prop drilling." This creates tight coupling and makes refactoring a chore. Lifting the state to a shared context breaks this chain.
Use global state for:
- User authentication: The user's ID, roles, and token. This is the canonical example.
- Application-wide settings: Theme (light/dark mode), language preference.
- Shared, cross-cutting data: Shopping cart contents, notifications, or a server-side data cache that multiple features rely on.
The Cost of Global State
Global state is powerful, but it's not free. The main cost is performance.
When a value in a global store changes, any component subscribed to that store might re-render. With a naive implementation like a single large React Context, every consuming component will re-render, even if the specific piece of data it cares about didn't change.
An interviewer might ask: "We have a global user object with 20 fields. A header component only displays the user's name. What happens when their email address changes?" The correct answer is that with a naive Context setup, the header re-renders unnecessarily. This demonstrates you understand the performance implications. The follow-up is about how to fix it, which involves either splitting contexts or using libraries with selectors (like Zustand or Redux) that let components subscribe to specific slices of state.
This re-render cascade is the central problem that advanced state management libraries are designed to solve.
A Minimal Global Store with Context
React Context is the built-in tool for global state. It's perfect for low-frequency updates like theme changes or user authentication.
Here is a complete, minimal example for a theme switcher. It shows the three core parts: creating the context, providing the state, and creating a hook for components to consume it.
import React, { createContext, useState, useContext, ReactNode } from 'react';
type Theme = 'light' | 'dark';
// The shape of the context value
interface ThemeContextType {
theme: Theme;
toggleTheme: () => void;
}
// 1. Create the Context with a default value
const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
// 2. Create the Provider component
// It holds the state and provides it to its children
export const ThemeProvider = ({ children }: { children: ReactNode }) => {
const [theme, setTheme] = useState<Theme>('light');
const toggleTheme = () => {
setTheme(currentTheme => (currentTheme === 'light' ? 'dark' : 'light'));
};
const value = { theme, toggleTheme };
return (
<ThemeContext.Provider value={value}>
{children}
</ThemeContext.Provider>
);
};
// 3. Create a custom hook for easy consumption
export const useTheme = () => {
const context = useContext(ThemeContext);
if (context === undefined) {
throw new Error('useTheme must be used within a ThemeProvider');
}
return context;
};
Any component wrapped in <ThemeProvider> can now call useTheme() to get the current theme and the function to change it, without any prop drilling.
Visualizing the Re-render Cascade
This flow shows why global state must be managed carefully. An action in one part of the app can have far-reaching effects.
This is desirable when the components are truly dependent on the changed state. It becomes a performance problem when a component re-renders because an unrelated part of the global state object was updated.
Try it
Take a simple app with a parent, a middle component, and a grandchild. Pass a prop from the parent to the grandchild. Now, refactor it to use React Context to provide the data directly to the grandchild, removing the prop from the middle component.
Next: We'll look at dedicated patterns and libraries for managing this complexity in "State Patterns: Redux, Zustand and Context".
Lock it in — apply this lesson under interview conditions.