What is State in React?
State represents dynamic data in a React application that can change over time and trigger UI updates. How and where you store this state determines your app’s performance, scalability, and complexity.
Local State
What is Local State?
Local state is data that belongs to a single component and is managed inside that component. It is not meant to be accessed or modified by other components.
How to Use Local State
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
Real-World Examples of Local State
- Form input values
- Toggle dropdowns or modals
- Tabs or accordion UI
- Button loading states
For example, a form created using an online tool like resume builders usually keeps input values as local state until submission.
Advantages of Local State
- Simple and easy to manage
- Better performance
- No external dependencies
Limitations of Local State
- Cannot be shared easily
- Leads to prop drilling if passed deeply
Global State
What is Global State?
Global state is shared data that multiple components across different parts of the app need access to.
Common Global State Examples
- User authentication details
- Theme (dark/light mode)
- Language preferences
- Shopping cart data
How to Implement Global State Using Context API
// ThemeContext.js
import { createContext, useState } from "react";
export const ThemeContext = createContext();
export function ThemeProvider({ children }) {
const [theme, setTheme] = useState("light");
return (
<ThemeContext.Provider value={{ theme, setTheme }}>
{children}
</ThemeContext.Provider>
);
}
// App.js
import { ThemeProvider } from "./ThemeContext";
function App() {
return (
<ThemeProvider>
<Dashboard />
</ThemeProvider>
);
}
This approach is useful for shared UI behavior such as templates used while generating professional documents.
Advantages of Global State
- Eliminates prop drilling
- Centralized state management
- Consistent data across components
Limitations of Global State
- More complexity
- Risk of unnecessary re-renders
- Harder to debug if overused
Local State vs Global State: Comparison
| Aspect Local State Global State | ||
| Scope | Single component | Entire application |
| Complexity | Low | Medium to High |
| Performance | High | Can degrade if misused |
| Best for | UI-specific logic | Shared business logic |
Best Practices & Special Notes
- Start with local state by default
- Promote state to global only when needed
- Avoid storing transient UI state globally
- Split global state logically to reduce re-renders
For deeper learning patterns and architecture tips, explore structured content on modern frontend development.
To test your understanding, interactive assessments like developer quizzes help reinforce concepts.
Final Takeaway
Local state keeps components simple and fast, while global state enables shared behavior across your app. Choosing the right one at the right time is a key skill that separates beginner React developers from advanced ones.