JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsreact
PrevNext

Learn the concept

State

react
junior
state

What is state in React and how is it different from props?

state
props
useState
data-management
fundamentals
Quick Answer

State is internal, mutable data managed within a component that triggers re-renders when changed. Props are external, read-only inputs passed from parent components. State is for data that changes over time; props are for configuring components.

Detailed Explanation

State:

  • Internal to the component
  • Mutable via setState/useState
  • Changes trigger re-renders
  • Managed by the component itself
  • Used for: form inputs, toggles, fetched data, UI state

Props:

  • External, passed from parent
  • Read-only (immutable)
  • Changes from parent trigger re-renders
  • Configured by parent component
  • Used for: configuration, callbacks, data passing

Key Rules:

  1. Never modify state directly (use setState/setX)
  2. State updates are batched and asynchronous — React 18+ automatically batches all updates, even inside setTimeout, promises, and native event handlers
  3. State updates are merged (class) or replaced (hooks)
  4. Lift state up when sharing between components

React 19 State Features:

  • useActionState — manage state with form actions (pending, error, result)
  • useOptimistic — show optimistic UI while async operations complete

Code Examples

State vs Props comparisonJSX
function Counter({ initialCount, step }) {  // Props: external config
  const [count, setCount] = useState(initialCount); // State: internal
  
  return (
    <div>
      <p>Count: {count}</p>
      {/* Props used for configuration */}
      <button onClick={() => setCount(count + step)}>
        Add {step}
      </button>
    </div>
  );
}

// Usage - props configure the component
<Counter initialCount={0} step={1} />
<Counter initialCount={100} step={10} />

Real-World Applications

Use Cases

Shopping Cart Management

Using state to track cart items, quantities, and totals with real-time UI updates

Multi-Step Form Wizard

Managing form state across multiple steps with validation and navigation

Real-Time Dashboard

State-driven dashboard components that update with WebSocket data

Mini Projects

Todo App with State Patterns

beginner

Build a todo app demonstrating useState, state lifting, and functional updates

Undo/Redo System

intermediate

Implement undo/redo functionality using state history with useReducer

Industry Examples

Figma

Uses React state for real-time collaborative UI updates in their design tool

Notion

Complex nested state management for block-based document editing

Resources

React Docs - State: A Component's Memory

docs

React Docs - Thinking in React

docs

Related Questions

Explain the useState and useEffect hooks and their common patterns.

mid
hooks
Previous
What are props in React and how do you pass data between components?
Next
Why are keys important when rendering lists in React?
PrevNext