When Props Change: Initial State Doesn't Update
You'll learn why changing a prop doesn't automatically update state that was initialized from that prop.
The Core Idea
When you initialize state from a prop like this:
function Counter({ startValue }) {
const [count, setCount] = useState(startValue);
// ...
}
The startValue prop is only read once — when the component first mounts. If the parent later passes a different startValue, your state doesn't magically update. The initial state was already set, and React doesn't re-initialize it on subsequent renders.
Why This Happens
Remember that "Initial State Runs Only Once" (from an earlier lesson)? The value or function you pass to useState is only evaluated during the first render. After that, React ignores it. Your state lives independently from props.
The Analogy
Think of initial state like writing your name on a birth certificate. Your parents (props) provided the name once. If they later change their minds and want to call you something else, the birth certificate doesn't automatically update — it was filled out once and is now independent.
Common Pitfall
function Greeting({ userName }) {
const [name, setName] = useState(userName); // Only uses userName on mount!
return <p>Hello, {name}</p>;
}
If userName prop changes from "Alice" to "Bob", the name state stays "Alice" unless you explicitly update it.
Solutions Preview
You'll later learn to handle this with the key prop (forcing a remount) or by using the prop directly instead of storing it in state.
Key Takeaway: Initial state is a one-time snapshot from props. Changing the prop won't reset state — you'd need to remount the component or manually sync the state.