JS Guide
HomeQuestionsSearchResources
Search

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsreact
PrevNext
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 may be asynchronous
  3. State updates are merged (class) or replaced (hooks)
  4. Lift state up when sharing between components

Code Examples

State vs Props comparison
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} />

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?