Unit 2: State Management & HooksLesson #3 / 5

Lesson 3: Component State & The useState Hook

Er. Manoj Kumar — AuthorEr. Manoj KumarLast Updated: 26 Aug, 2026

1. Component State & useState

While props allow parents to pass data in, state holds private, local component data that can change over time in response to user actions.

  • Triggering Re-renders: Calling the state setter function (setCount) instructs React to schedule a re-render with the updated value.
  • Never Mutate State Directly: Always use the setter function rather than modifying variables like count = 5.
  • Functional State Updates: When new state depends on previous state, pass an updater callback setCount(prev => prev + 1).
  • Code Example
    import React, { useState } from 'react';
    export default function InteractiveCounter() {
    const [count, setCount] = useState(0);
    const [theme, setTheme] = useState("dark");
    return (
    <div style={{ padding: '20px', background: theme === 'dark' ? '#17181c' : '#fff' }}>
    <h3 style={{ color: '#04AA6D' }}>Current Count: {count}</h3>
    <div style={{ display: 'flex', gap: '8px' }}>
    <button onClick={() => setCount(prev => prev + 1)}>Increment (+)</button>
    <button onClick={() => setCount(prev => prev - 1)}>Decrement (-)</button>
    <button onClick={() => setCount(0)}>Reset</button>
    </div>
    </div>
    );
    }

    Interactive Knowledge Check

    Test your understanding of Lesson #3 concepts

    What does the useState hook return when called in a functional component?