0
0
Reactframework~20 mins

Rendering lists in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React List Master
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 fruits:

function FruitList() {
  const fruits = ['Apple', 'Banana', 'Cherry'];
  return (
    <ul>
      {fruits.map((fruit, index) => <li key={index}>{fruit}</li>)}
    </ul>
  );
}

What will be rendered in the browser?

React
function FruitList() {
  const fruits = ['Apple', 'Banana', 'Cherry'];
  return (
    <ul>
      {fruits.map((fruit, index) => <li key={index}>{fruit}</li>)}
    </ul>
  );
}
A<ul><li>AppleBananaCherry</li></ul>
B<ul><li>0</li><li>1</li><li>2</li></ul>
C<ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul>
D<ul><li>fruit</li><li>fruit</li><li>fruit</li></ul>
Attempts:
2 left
💡 Hint

Look at what is inside the <li> tags inside the map function.

📝 Syntax
intermediate
2:00remaining
Which option correctly renders a list of numbers in React?

You want to render a list of numbers from 1 to 3 in React. Which code snippet will work without errors and render the numbers as list items?

A
const numbers = [1, 2, 3];
return &lt;ul&gt;{numbers.map(n =&gt; &lt;li key={n}&gt;{n}&lt;/li&gt;)}&lt;/ul&gt;;
B
const numbers = [1, 2, 3];
return &lt;ul&gt;{numbers.map(n =&gt; &lt;li&gt;{n}&lt;/li&gt;)}&lt;/ul&gt;;
C
const numbers = [1, 2, 3];
return &lt;ul&gt;{numbers.forEach(n =&gt; &lt;li key={n}&gt;{n}&lt;/li&gt;)}&lt;/ul&gt;;
D
const numbers = [1, 2, 3];
return &lt;ul&gt;{numbers.map((n, index) =&gt; &lt;li key={index}&gt;{n}&lt;/li&gt;)}&lt;/ul&gt;;
Attempts:
2 left
💡 Hint

Remember that map returns a new array, but forEach does not.

🔧 Debug
advanced
2:00remaining
Why does this React list rendering cause a warning?

Look at this React component:

function Colors() {
  const colors = ['red', 'green', 'blue'];
  return (
    <ul>
      {colors.map(color => <li>{color}</li>)}
    </ul>
  );
}

When running this code, React shows a warning about keys. What is the cause?

React
function Colors() {
  const colors = ['red', 'green', 'blue'];
  return (
    <ul>
      {colors.map(color => <li>{color}</li>)}
    </ul>
  );
}
AThe component is missing a wrapping div around the list.
BThe array 'colors' is empty, so nothing renders.
CThe map function is missing a return statement, causing a syntax error.
DEach list item is missing a unique 'key' prop, which React needs to track items efficiently.
Attempts:
2 left
💡 Hint

React requires a special prop on list items to help identify them uniquely.

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

Consider this React component that renders a list and a button to add an item:

import { useState } from 'react';

function TodoList() {
  const [todos, setTodos] = useState(['Task 1', 'Task 2']);

  function addTodo() {
    setTodos([...todos, 'Task 3']);
  }

  return (
    <div>
      <ul>
        {todos.map((todo, i) => <li key={i}>{todo}</li>)}
      </ul>
      <button onClick={addTodo}>Add Task</button>
    </div>
  );
}

What will the list show after clicking the 'Add Task' button once?

React
import { useState } from 'react';

function TodoList() {
  const [todos, setTodos] = useState(['Task 1', 'Task 2']);

  function addTodo() {
    setTodos([...todos, 'Task 3']);
  }

  return (
    <div>
      <ul>
        {todos.map((todo, i) => <li key={i}>{todo}</li>)}
      </ul>
      <button onClick={addTodo}>Add Task</button>
    </div>
  );
}
A<ul><li>Task 1</li><li>Task 2</li><li>Task 3</li></ul>
B<ul><li>Task 1</li><li>Task 2</li></ul>
C<ul><li>Task 3</li></ul>
D<ul><li>Task 1</li><li>Task 2</li><li>Task 3</li><li>Task 3</li></ul>
Attempts:
2 left
💡 Hint

Think about how the state updates when the button is clicked.

🧠 Conceptual
expert
2:00remaining
Why is using array index as key in list rendering sometimes problematic?

In React, when rendering lists, developers often use the array index as the key prop like this:

{items.map((item, index) => <li key={index}>{item}</li>)}

Why can this cause problems in some cases?

AUsing index as key causes syntax errors in React and prevents rendering.
BUsing index as key can cause incorrect UI updates when list items are reordered, inserted, or deleted because keys should uniquely identify items across renders.
CUsing index as key makes React render the list items twice, causing performance issues.
DUsing index as key disables React's ability to handle events on list items.
Attempts:
2 left
💡 Hint

Think about what happens when the order of items changes but keys stay the same.