0
0
Reactframework~20 mins

Importance of keys in React - Challenge Your Understanding

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

Consider a React component rendering a list of items. What is the main reason React requires a unique key prop for each list item?

ATo help React identify which items have changed, been added, or removed for efficient updates.
BTo style each list item differently using CSS selectors.
CTo prevent the list from rendering on the screen.
DTo make the list items accessible to screen readers automatically.
Attempts:
2 left
💡 Hint

Think about how React updates the screen when data changes.

state_output
intermediate
2:00remaining
What happens if keys are missing or duplicated?

Given this React code snippet rendering a list without unique keys, what is the likely outcome when the list changes?

const items = ['apple', 'banana', 'cherry'];
return items.map(item => <li>{item}</li>);
AReact will automatically assign unique keys internally with no issues.
BReact will throw a syntax error and stop rendering.
CReact may re-render all items inefficiently and cause unexpected UI bugs.
DThe list will not render at all on the page.
Attempts:
2 left
💡 Hint

What does React warn about in the console when keys are missing?

📝 Syntax
advanced
2:00remaining
Identify the correct way to assign keys in a list

Which option correctly assigns keys to list items in React?

const fruits = ['apple', 'banana', 'cherry'];
return fruits.map((fruit, index) => /* fill here */ );
A<li key={fruit}>{fruit}</li>
B<li key={index}>{fruit}</li>
C<li key={fruit + index}>{fruit}</li>
D<li key={Math.random()}>{fruit}</li>
Attempts:
2 left
💡 Hint

Keys should be stable and unique for each item.

🔧 Debug
advanced
2:00remaining
Why does this React list cause UI bugs?

Look at this React component:

function TodoList({ todos }) {
  return (
    <ul>
      {todos.map((todo, i) => (
        <li key={i}>{todo.text}</li>
      ))}
    </ul>
  );
}

When items are added or removed, the UI sometimes shows wrong todo texts. Why?

AThe <code>text</code> property does not exist on todo objects.
BThe <code>key</code> prop is missing entirely, causing errors.
CThe <code>todos</code> array is empty, so nothing renders.
DUsing index as key causes React to confuse items when list order changes.
Attempts:
2 left
💡 Hint

Think about what happens when list items move but keys stay the same.

🧠 Conceptual
expert
3:00remaining
What is the impact of keys on React's reconciliation algorithm?

React uses keys during reconciliation to optimize updates. What exactly does React do with keys to improve performance?

AReact uses keys to sort elements alphabetically before rendering.
BReact uses keys to match old and new elements, minimizing DOM changes by reusing elements when keys match.
CReact ignores keys during reconciliation and updates all elements every time.
DReact uses keys to assign CSS classes dynamically to elements.
Attempts:
2 left
💡 Hint

Consider how React decides which elements to keep or replace when data changes.