React · Middle · Question 59 of 75
Which state variables are needed here?
const [items, setItems] = useState(list);
const [selectedItem, setSelectedItem] = useState(list[0]);- ✓Correct answer: Store
selectedIdinstead of a copy of the item, and find the item fromitems - 2Both are fine; duplicating objects in state is recommended
- 3Use a single global variable instead
Explanation
If an item in items is edited, the copy in selectedItem becomes stale. Storing the id avoids duplicated, out-of-sync state.