What is State in React?
State is data that changes over time and affects what gets rendered on the screen. Unlike props which are passed from parent to child, state is internal to a component and can be updated using setter functions.
useState – Local State
State with Objects
State with Arrays
useReducer – Complex State Logic
Complex useReducer Example
Context API – Global State
State Lifting
State Management Libraries Comparison
| Solution | Best For | Complexity | Bundle Size |
|---|---|---|---|
| useState | Simple local state | Low | 0 KB (built-in) |
| useReducer | Complex local state logic | Low | 0 KB (built-in) |
| Context API | Global state (low frequency) | Low | 0 KB (built-in) |
| Redux | Large apps with complex state | High | ~12 KB |
| Zustand | Simple global state | Medium | ~3 KB |
| Jotai | Atomic state management | Medium | ~3 KB |
| Recoil | React-specific state | Medium | ~15 KB |
State Update Best Practices
- Never mutate state directly – Always use setter functions
- Use functional updates when new state depends on previous state
- Batch related state into a single object when updates are related
- Keep state minimal – Derive values when possible
- Lift state up when multiple components need access
- Use useReducer for complex state transitions
- Memoize selectors with useMemo to prevent unnecessary recalculations
Common Pitfalls
Derived State
Conclusion
React provides multiple tools for state management. Start with useState for local state, useReducer for complex logic, and Context API for global state. For large applications, consider dedicated state management libraries like Zustand or Redux.