Course contentsShow
← React
Lesson 332 of 2,39610. useState BasicsFree lesson

Calling the Setter Function

How to update state by calling the setter function and what happens when you do.

Calling the Setter Function

What you'll learn: How to actually update state by calling the setter function you receive from useState.

The Core Mechanism

When you destructure useState, you get two things: the current value and a setter function. To update state, you simply call that setter with the new value you want. React will then:

  1. Store the new value
  2. Re-render your component with that updated value

Think of the setter like a "save button" — calling it tells React "here's my new state, update the UI accordingly."

Basic Usage

const [count, setCount] = useState(0);

// To update count, call setCount with the new value:
setCount(5); // count becomes 5
setCount(10); // count becomes 10
setCount(count + 1); // increments current value

The setter name always starts with set by convention, followed by the capitalized state variable name.

What Happens When You Call It

The moment you call the setter:

  • React schedules a re-render
  • The component function runs again
  • useState returns the new value instead of the old one
  • Your JSX reflects the updated state

In Event Handlers

You'll typically call setters inside callbacks:

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <button onClick={() => setCount(count + 1)}>
      Clicked {count} times
    </button>
  );
}

Each click calls setCount, which updates the state and re-renders the button with the new count.

Key Takeaway: Calling the setter function is how you trigger state updates — pass it the new value, and React handles re-rendering your component with that updated state.