Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
Frontend System Design Roadmap
  1. 1.HTTP/1.1 vs HTTP/2 vs HTTP/3 (QUIC)
  2. 2.TCP and TLS Handshakes: Connection Setup Costs
  3. 3.Browser Network Stack and Connection Pooling
  4. 4.GET vs POST and Idempotency in HTTP
  5. 5.The Critical Rendering Path (CRP)
  6. 6.DOM, CSSOM and the Render Tree
  7. 7.Layout, Paint and Compositing
  8. 8.Reflows and Repaints Explained
  9. 9.Event Loop: Call Stack, Task and Microtask Queues
  10. 10.Async JavaScript: Promises and async/await
  11. 11.Web Workers: Multi-threading in the Browser
  12. 12.Browser Storage: Cookies, Web Storage, IndexedDB
  13. 13.Client-Side Storage: Limits and Security Risks
  14. 14.Choosing the Right Client-Side Storage
  15. 15.HTTP Caching: Cache-Control, ETag, Last-Modified
  16. 16.Browser-Level Caching Mechanisms
  17. 17.Cache Invalidation for Frontend Apps
  18. 18.Structuring a Large Single-Page App
  19. 19.Component-Based Architecture Principles
  20. 20.Props, State and Component Lifecycle
  21. 21.Module Bundling Basics
  22. 22.Core Web Vitals: LCP, INP and CLS
  23. 23.FCP and Time to Interactive (TTI) Explained
  24. 24.Measuring Performance: Lighthouse and WebPageTest
  25. 25.Asset Compression and Minification Basics
  26. 26.What Is a CDN and How Does It Work?
  27. 27.CDN Benefits: Latency, Availability, Offload
  28. 28.CDN Cache Behavior and Invalidation
  29. 29.Global State vs Local Component State
  30. 30.State Patterns: Redux, Zustand and Context
  31. 31.Data Fetching: React Query and SWR
  32. 32.Immutable Data in Frontend State
  33. 33.SSR vs CSR: Rendering Trade-offs
  34. 34.Hydration and Time to First Byte (TTFB)
  35. 35.Static Site Generation (SSG) for Speed and SEO
  36. 36.Incremental Static Regeneration (ISR)
  37. 37.Micro-Frontends: Core Principles
  38. 38.Micro-Frontend Integration Strategies
  39. 39.Communication Between Micro-Frontends
  40. 40.Micro-Frontends: Pros and Cons for Large Teams
  41. 41.Real-Time: Polling, SSE and WebSockets
  42. 42.Real-Time Use Cases: Chat, Alerts, Live Data
  43. 43.Designing a Real-Time Data Flow
  44. 44.Scaling WebSocket Connections
  45. 45.Responsive Images: srcset, sizes and picture
  46. 46.Modern Image Formats: WebP, AVIF, JPEG XL
  47. 47.Lazy Loading Images and Videos
  48. 48.Video Streaming: HLS, DASH, Adaptive Bitrate
  49. 49.REST and GraphQL for Frontend Engineers
  50. 50.Batching, Throttling and Debouncing API Calls
  51. 51.Handling API Errors in the Frontend
  52. 52.Authentication Flows: OAuth and JWT
  53. 53.Global Error Boundaries in React
  54. 54.Error Reporting: Sentry and LogRocket
  55. 55.Graceful Degradation and Fallback UIs
  56. 56.Real User Monitoring (RUM)
  57. 57.CORS: Cross-Origin Resource Sharing
  58. 58.Preventing XSS (Cross-Site Scripting)
  59. 59.CSRF Protection for Frontend Apps
  60. 60.Content Security Policy (CSP)
  61. 61.Storing Auth Tokens: Cookies vs localStorage
  62. 62.PWA Manifest and Installability
  63. 63.Service Workers: Lifecycle and Caching Strategies
  64. 64.Offline Support and Background Sync
  65. 65.Push Notifications for Web Apps
  66. 66.Webpack vs Vite vs Parcel
  67. 67.Code Splitting, Tree Shaking, Asset Optimization
  68. 68.Module Resolution and Hot Module Replacement
  69. 69.Monorepo Tooling: Nx and Lerna
  70. 70.Atomic Design for Component Libraries
  71. 71.Theming and Customization in Design Systems
  72. 72.Accessibility in Component Libraries
  73. 73.Component Library Docs, Testing and Versioning
  74. 74.Publishing and Consuming a Component Library
  75. 75.Full-Text Search: Levenshtein and Fuzzy Matching
  76. 76.Indexing Large Datasets in the Browser
  77. 77.Search Performance: Debouncing and Web Workers
  78. 78.Integrating Backend Search Services
  79. 79.Choosing a Chart Library: D3, Chart.js, Recharts
  80. 80.Rendering Large Datasets: Canvas vs SVG
  81. 81.Interactive, Responsive Data Visualizations
  82. 82.Data Fetching and Aggregation for Dashboards
  83. 83.A/B Test Design and Rollout Strategies
  84. 84.Client-Side vs Server-Side A/B Testing
  85. 85.Feature Flags and Remote Configuration
  86. 86.Experiment Data Collection and Analytics
Frontend System Design Roadmap
29

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:

  1. 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.
  2. 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.

Try a free mock interviewPractice system design problems
Previous

CDN Cache Behavior and Invalidation

Next

State Patterns: Redux, Zustand and Context