Custom Hooks in React
15 questions · 12 min · Medium
Test your knowledge of lists and keys in ReactJS, including rendering arrays with map(), assigning unique keys, using stable identifiers, handling list updates, filtering data, and understanding how keys affect React reconciliation.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
15 questions · 12 min · Medium
20 questions · 15 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
B. map()
The map() method transforms each item in an array into a JSX element, producing a collection that React can render.
A. To help identify elements between renders
Keys help React match list items across renders so it can determine which items were added, removed, or reordered.
C. <li key={user.id}>{user.name}</li>
A key should use a stable identifier associated with the item, such as a unique user ID, when one is available.
D. On the outermost element returned for each list item
React needs the key on the element directly produced for each item in the mapped list. If a component is returned, the key generally belongs on that component element.
B. React may associate component state with the wrong item
When positions change, index-based keys can cause React to match an existing component instance with a different logical item, potentially producing incorrect state or UI behavior.
A. A stable, unique identifier for each item
Stable unique identifiers allow React to track the same logical items across renders, even when their positions change.
C. No, key is a special React attribute and is not available as props.key
React uses key internally for reconciliation and does not pass it as a normal prop. Pass the identifier separately if the child component needs it.
D. React may produce a warning and reconciliation behavior may be incorrect
Keys must be unique among siblings. Duplicate keys make it difficult for React to identify elements correctly during updates.
B. {products.map(product => <li key={product.id}>{product.name}</li>)}
The map() method returns a new array of JSX elements, and each element uses the product's stable ID as its key.
A. Yes, React can render an array of elements
React supports rendering arrays of elements as children, which makes map() a common pattern for rendering lists.
C. To help React identify which item should be removed
Stable keys help React determine which item disappeared from the list and update the rendered output accordingly.
D. Use a keyed React Fragment or a wrapper element for each item
When an item returns multiple sibling elements, a keyed Fragment using React.Fragment can group them without adding an extra DOM element.
B. It can cause React to treat items as new elements on each render
Random keys change between renders, so React may recreate components instead of preserving their instances, potentially losing local state and causing unnecessary work.
A. filter()
The filter() method creates an array containing only items that satisfy a condition. The resulting array can then be rendered using map().
C. A key helps React with reconciliation, while id is an ordinary attribute that can appear in the DOM
React uses key internally to track elements during reconciliation. The id attribute is a regular DOM attribute commonly used for identification, styling, or linking.