Lists and Keys — Concept Check
Questions on Lists and Keys. A key tells React which item is which between renders. Using the array index is the single most common source of "the wrong row updated" bugs.
Questions in this quiz
8 questions on Lists and Keys — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have a list of tasks rendered in React, each with a unique id. If you remove a task from the beginning of the list, what ensures that the remaining tasks retain their state?
- AUsing the array index as the key
- BUsing Math.random() as the key
- CUsing a composite key with task name
- DUsing the task's id as the key
2.In a React application, you notice that deleting an item from a list causes the wrong item to lose its state. What is the likely cause?
- AKeys are set using a composite key
- BKeys are unique among siblings
- CKeys are set using array indices
- DKeys are set using a stable id
3.Why might using Math.random() as a key in a React list lead to performance issues?
- AIt generates a new key every render, causing React to destroy and rebuild the list
- BIt is a stable key that does not change
- CIt ensures keys are unique among siblings
- DIt prevents items from reordering correctly
4.A developer uses a composite key for a list of user comments in React. Which of the following is a valid composite key?
- Akey={`${comment.userId}-${comment.timestamp}`}
- Bkey={comment.id}
- Ckey={Math.random()}
- Dkey={index}
5.You have a list of products that never changes in order or size. Which key strategy is safe to use?
- AUsing a composite key
- BUsing the product's id as the key
- CUsing the array index as the key
- DUsing Math.random() as the key
6.A React list of messages uses the sender's username as a key. What potential issue might arise?
- AUsernames are stable and cause no issues
- BUsernames are always unique among siblings
- CUsing usernames improves performance
- DUsernames may not be unique, causing key conflicts
7.What is a primary reason to avoid using array indices as keys in a React list?
- AIndices improve rendering performance
- BIndices are stable and do not change
- CIndices are always unique
- DIndices shift when items are removed, causing state to attach incorrectly
8.In a React application, switching between two lists with the same keys causes unexpected behavior. Why?
- AKeys are globally unique
- BKeys should use Math.random()
- CKeys must be unique among siblings, not globally
- DKeys should use array indices