0
0
Reactframework~20 mins

Common list rendering mistakes in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
List Rendering Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this React component?

Consider this React component that renders a list of items:

function ItemList() {
  const items = ['apple', 'banana', 'cherry'];
  return (
    <ul>
      {items.map((item) => <li>{item}</li>)}
    </ul>
  );
}

What problem will this cause in the browser console?

React
function ItemList() {
  const items = ['apple', 'banana', 'cherry'];
  return (
    <ul>
      {items.map((item) => <li key={item}>{item}</li>)}
    </ul>
  );
}
ANo problem; the list renders fine without warnings.
BWarning: Each child in a list should have a unique "key" prop.
CError: JSX elements cannot be returned without a wrapping element.
DWarning: The map function must return a string, not JSX elements.
Attempts:
2 left
💡 Hint

React needs a way to identify each list item uniquely for efficient updates.

📝 Syntax
intermediate
2:00remaining
Which option correctly fixes the syntax error in this list rendering code?

This React code tries to render a list but has a syntax error:

const numbers = [1, 2, 3];
return (
  <ul>
    {numbers.map(number => <li key={number}>{number}<li>)}
  </ul>
);

Which option fixes the syntax error?

React
const numbers = [1, 2, 3];
return (
  <ul>
    {numbers.map(number => <li key={number}>{number}</li>)}
  </ul>
);
A{numbers.map(number => <li key={number}>{number}</li>)}
B{numbers.map(number => <li key={number}>{number}</li></li>)}
C{numbers.map(number => <li key={number}>{number}</li>);}
D{numbers.map(number => <li key={number}>{number})}</li>}
Attempts:
2 left
💡 Hint

Check that all JSX tags are properly closed.

🔧 Debug
advanced
2:00remaining
Why does this React list rendering cause unexpected behavior?

Look at this React component:

function TodoList() {
  const todos = [
    { id: 1, task: 'Wash dishes' },
    { id: 2, task: 'Do laundry' },
    { id: 3, task: 'Cook dinner' }
  ];

  return (
    <ul>
      {todos.map((todo, index) => (
        <li key={index}>{todo.task}</li>
      ))}
    </ul>
  );
}

What problem can arise from using index as the key?

React
function TodoList() {
  const todos = [
    { id: 1, task: 'Wash dishes' },
    { id: 2, task: 'Do laundry' },
    { id: 3, task: 'Cook dinner' }
  ];

  return (
    <ul>
      {todos.map((todo, index) => (
        <li key={index}>{todo.task}</li>
      ))}
    </ul>
  );
}
AUsing index as key causes React to ignore the list and render nothing.
BUsing index as key causes a syntax error in React.
CUsing index as key will prevent the list from rendering at all.
DUsing index as key can cause incorrect item updates when the list changes order or items are added/removed.
Attempts:
2 left
💡 Hint

Think about what happens if the list items reorder or change.

state_output
advanced
2:00remaining
What will be the output after clicking the button in this React component?

Examine this React component:

import { useState } from 'react';

function CounterList() {
  const [counts, setCounts] = useState([0, 0, 0]);

  function incrementFirst() {
    counts[0] += 1;
    setCounts(counts);
  }

  return (
    <div>
      <ul>
        {counts.map((count, i) => <li key={i}>Count {i + 1}: {count}</li>)}
      </ul>
      <button onClick={incrementFirst}>Increment First</button>
    </div>
  );
}

What happens when the button is clicked?

React
import { useState } from 'react';

function CounterList() {
  const [counts, setCounts] = useState([0, 0, 0]);

  function incrementFirst() {
    counts[0] += 1;
    setCounts(counts);
  }

  return (
    <div>
      <ul>
        {counts.map((count, i) => <li key={i}>Count {i + 1}: {count}</li>)}
      </ul>
      <button onClick={incrementFirst}>Increment First</button>
    </div>
  );
}
AThe first count number does not update in the UI because React state was mutated directly.
BThe component crashes with a runtime error when clicking the button.
CThe first count number updates and the UI re-renders showing the incremented value.
DAll count numbers reset to zero after clicking the button.
Attempts:
2 left
💡 Hint

React state should not be mutated directly; think about how React detects changes.

🧠 Conceptual
expert
2:00remaining
Which option best explains why keys are critical in React list rendering?

Why does React require a unique key for each element in a list?

AKeys prevent React from rendering duplicate list items by filtering them out automatically.
BKeys are used to style list items differently based on their position.
CKeys help React identify which items have changed, been added, or removed to optimize rendering performance.
DKeys allow React to convert lists into arrays internally for faster processing.
Attempts:
2 left
💡 Hint

Think about how React updates the UI efficiently when data changes.