0
0
Reactframework~20 mins

Keys concept in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React Keys Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Why use keys in React lists?

Consider a React component rendering a list of items. Why is it important to assign a unique key prop to each list item?

React
function ItemList({ items }) {
  return (
    <ul>
      {items.map(item => <li key={item.id}>{item.name}</li>)}
    </ul>
  );
}
AKeys prevent JavaScript errors by validating list item types.
BKeys help React identify which items changed, added, or removed, improving rendering performance.
CKeys are only needed when list items have event handlers.
DKeys are used to style list items uniquely with CSS.
Attempts:
2 left
💡 Hint

Think about how React updates the UI when data changes.

state_output
intermediate
2:00remaining
What happens if keys are not unique?

Given this React code, what will be the output behavior when the list updates?

React
function FruitList() {
  const [fruits, setFruits] = React.useState(['apple', 'banana', 'cherry']);

  React.useEffect(() => {
    setTimeout(() => {
      setFruits(['banana', 'apple', 'cherry']);
    }, 1000);
  }, []);

  return (
    <ul>
      {fruits.map(fruit => <li key={fruit}>{fruit}</li>)}
    </ul>
  );
}
AReact will reorder the list correctly because keys are unique strings.
BReact will throw a runtime error due to duplicate keys.
CReact will not update the list properly because all keys are the same, causing rendering bugs.
DReact will ignore the keys and update the list normally.
Attempts:
2 left
💡 Hint

What happens if React cannot distinguish list items by keys?

📝 Syntax
advanced
2:00remaining
Identify the correct key usage in this React list

Which option correctly assigns keys in this React list rendering?

React
const users = [{id: 1, name: 'Anna'}, {id: 2, name: 'Ben'}, {id: 3, name: 'Cara'}];

function UserList() {
  return (
    <ul>
      {users.map(user => (
        <li key={/* key here */}>{user.name}</li>
      ))}
    </ul>
  );
}
Akey={user.id}
Bkey={users.indexOf(user)}
Ckey={Math.random()}
Dkey={user.name}
Attempts:
2 left
💡 Hint

Keys should be stable and unique for each item.

🔧 Debug
advanced
2:00remaining
Find the cause of the warning about keys

Why does React show this warning: "Each child in a list should have a unique 'key' prop."?

React
function TodoList({ todos }) {
  return (
    <ul>
      {todos.map(todo => <li>{todo.text}</li>)}
    </ul>
  );
}
ABecause the <code>ul</code> element is missing an <code>id</code> attribute.
BBecause the <code>todos</code> array is empty.
CBecause the <code>todo.text</code> is not unique.
DBecause the <code>li</code> elements lack the <code>key</code> prop.
Attempts:
2 left
💡 Hint

Check the list items inside the map function.

🧠 Conceptual
expert
3:00remaining
Why is using array index as key discouraged in dynamic lists?

In React, why is it generally discouraged to use the array index as the key prop when rendering dynamic lists?

ABecause using index as key can cause incorrect component state and inefficient re-renders when list items change order or are added/removed.
BBecause array indexes are not unique and cause syntax errors.
CBecause React requires keys to be strings, and indexes are numbers.
DBecause using index as key disables React's virtual DOM.
Attempts:
2 left
💡 Hint

Think about what happens when list items move or change.