Initial State Runs Only Once
What you'll learn: Why React only evaluates the initial state argument on the first render, saving you from unnecessary work on every re-render.
The Core Idea
When you call useState(initialValue), React uses that initialValue to set up the state variable on the first render only. On every subsequent re-render, React ignores the initial state argument completely—it already has the current state value stored internally.
This is crucial to understand: the initial state is just a starting point. Once the component has rendered once, that argument to useState is never used again for that component instance.
Why This Matters
Imagine you write:
const [count, setCount] = useState(0);
When your component re-renders (maybe because you called setCount(1)), React doesn't reset count back to 0. It remembers the current value (1) and ignores the 0 you passed to useState.
A Helpful Analogy
Think of useState's initial value like filling out a registration form. You write your name once when you sign up. Every time you log back in afterward, the system doesn't ask you to re-enter your registration information—it already has it stored. The initial state is your "sign-up" information; React keeps it after that.
What About Expensive Initial Values?
Since the initial value only matters once, you might wonder: "What if calculating that initial value is slow?" Good instinct! React has a solution for that (using a function instead of a value), but for now, just know that React does run that initial state expression on every render—even though it ignores the result after the first one. The expression still executes.
Key Takeaway: The initial state argument to useState is only used on the first render—React ignores it on re-renders and preserves the current state value instead.