React · Junior · Question 28 of 75

Which rule of hooks is broken?

function Profile({ id }) {
  if (!id) return null;
  const [data, setData] = useState(null);
  // ...
}

Explanation

React matches state to hooks by their call order. If a hook isn't called during some render (because of an early return, a condition or a loop), the order breaks. Call hooks at the top level, before any conditional exits.

Further reading

Problem with a question

React · Junior · question 28

Test yourself

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

Take this quiz