React · Middle · Question 59 of 75

Which state variables are needed here?

const [items, setItems] = useState(list);
const [selectedItem, setSelectedItem] = useState(list[0]);

Explanation

If an item in items is edited, the copy in selectedItem becomes stale. Storing the id avoids duplicated, out-of-sync state.

Further reading

Problem with a question

React · Middle · question 59

Test yourself

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

Take this quiz