State Management · Middle · Question 3 of 75

Why can storing a copy of a prop in state be a bug?

function Message({ color }) {
  const [c, setC] = useState(color);
  return <p style={{ color: c }}>Hi</p>;
}

Explanation

"Mirroring" props in state freezes the first value. If the component should follow the prop, use it directly. Copy into state only when you intentionally need an initial value (and name it initialColor).

Further reading

Problem with a question

State Management · Middle · question 3

Test yourself

Take the State Management · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz