Skip to content
beginnerPhase 36 · React

Rendering Lists

Render arrays of data with map and handle dynamic lists efficiently.

30m
0 problems
Topic Progress0%

Rendering with map

Rendering with map

Use JavaScript's map method to render lists of elements.

Basic Map

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

// Usage
<TodoList
  todos={[
    { id: 1, text: "Learn React" },
    { id: 2, text: "Build app" },
    { id: 3, text: "Deploy" }
  ]}
/>

Map with Index

function NumberedList({ items }) {
  return (
    <ol>
      {items.map((item, index) => (
        <li key={index}>
          {index + 1}. {item}
        </li>
      ))}
    </ol>
  );
}

Complex List Items

function UserList({ users }) {
  return (
    <div className="user-list">
      {users.map((user) => (
        <div key={user.id} className="user-card">
          <img src={user.avatar} alt={user.name} />
          <h3>{user.name}</h3>
          <p>{user.email}</p>
        </div>
      ))}
    </div>
  );
}

Nested Lists

function CategoryList({ categories }) {
  return (
    <div>
      {categories.map((category) => (
        <div key={category.id}>
          <h2>{category.name}</h2>
          <ul>
            {category.items.map((item) => (
              <li key={item.id}>{item.name}</li>
            ))}
          </ul>
        </div>
      ))}
    </div>
  );
}

Filtering

Filtering

Use filter to show only certain items.

Basic Filter

function ActiveTodoList({ todos }) {
  const activeTodos = todos.filter((todo) => !todo.done);

  return (
    <ul>
      {activeTodos.map((todo) => (
        <li key={todo.id}>{todo.text}</li>
      ))}
    </ul>
  );
}

Filter with Search

function SearchableList({ items }) {
  const [search, setSearch] = useState("");

  const filteredItems = items.filter((item) =>
    item.name.toLowerCase().includes(search.toLowerCase())
  );

  return (
    <div>
      <input
        value={search}
        onChange={(e) => setSearch(e.target.value)}
        placeholder="Search..."
      />
      <ul>
        {filteredItems.map((item) => (
          <li key={item.id}>{item.name}</li>
        ))}
      </ul>
    </div>
  );
}

Filter and Map Together

function Stats({ numbers }) {
  const evenNumbers = numbers.filter((n) => n % 2 === 0);
  const doubled = evenNumbers.map((n) => n * 2);

  return (
    <div>
      <p>Even numbers: {evenNumbers.join(", ")}</p>
      <p>Doubled: {doubled.join(", ")}</p>
    </div>
  );
}

Conditional Rendering in Map

function NotificationList({ notifications }) {
  return (
    <ul>
      {notifications.map((notification) => (
        <li key={notification.id}>
          {notification.read ? (
            <span>{notification.message}</span>
          ) : (
            <strong>{notification.message}</strong>
          )}
        </li>
      ))}
    </ul>
  );
}

List Performance

List Performance

Key Requirements

  • Keys should be unique among siblings
  • Keys should be stable (not changing)
  • Avoid using array index as key (if list can reorder)

Performance Tips

// Bad: Creating new array on every render
function TodoList({ todos }) {
  return (
    <ul>
      {todos
        .filter((t) => !t.done) // Creates new array
        .map((todo) => (
          <li key={todo.id}>{todo.text}</li>
        ))}
    </ul>
  );
}

// Better: Memoize filtered list
function TodoList({ todos }) {
  const activeTodos = useMemo(
    () => todos.filter((t) => !t.done),
    [todos]
  );

  return (
    <ul>
      {activeTodos.map((todo) => (
        <li key={todo.id}>{todo.text}</li>
      ))}
    </ul>
  );
}

Virtualization for Large Lists

// For very large lists (1000+ items)
// Consider using react-window or react-virtualized

import { FixedSizeList } from "react-window";

function LargeList({ items }) {
  const Row = ({ index, style }) => (
    <div style={style}>
      {items[index].name}
    </div>
  );

  return (
    <FixedSizeList
      height={600}
      width="100%"
      itemCount={items.length}
      itemSize={35}
    >
      {Row}
    </FixedSizeList>
  );
}

Avoid These Mistakes

// Bad: No key
{items.map((item) => <li>{item.name}</li>)}

// Bad: Using index as key for reorderable list
{items.map((item, index) => <li key={index}>{item.name}</li>)}

// Bad: Math.random() as key
{items.map((item) => <li key={Math.random()}>{item.name}</li>)}

Practice Problems

0/3solved
Build Rendering Lists Component

Create a reusable React component implementing Rendering Lists. Include proper state management and accessibility.

Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where needed
Rendering Lists Testing

Write unit and integration tests for Rendering Lists using React Testing Library.

Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility tests
Rendering Lists Performance

Optimize Rendering Lists for performance. Consider memoization, code splitting, and bundle size.

Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysis

Quiz

1. What method do you use to render a list?

Question 1 options

2. Why should you avoid using array index as key?

Question 2 options

3. What should keys be?

Question 3 options

4. How do you filter a list?

Question 4 options

Flashcards

Question

What method renders lists in React?

Answer

The map() method

Question

What should keys be?

Answer

Unique among siblings and stable

Question

When should you avoid array index as key?

Answer

When the list can be reordered or items added/removed

Question

How do you filter a list in React?

Answer

Use filter() method before map()

Question

What is Rendering Lists?

Answer

Rendering Lists is a key concept in frontend development.

Revision Notes

Key Takeaways

  • 1.Use map() to render lists
  • 2.Keys must be unique and stable
  • 3.Avoid array index as key when possible
  • 4.Use filter() to filter lists
  • 5.Memoize expensive list operations

Interview Tips

  • Show how to render a list with map
  • Explain why keys are important
  • Demonstrate filtering and searching

Cheat Sheet

Cheat Sheet

Basic List

<ul>
  {items.map(item => (
    <li key={item.id}>{item.name}</li>
  ))}
</ul>

Filtered List

{items
  .filter(item => item.active)
  .map(item => <li key={item.id}>{item.name}</li>)
}

Key Rules

  • Unique among siblings
  • Stable (not changing)
  • Avoid array index if list reorders

Performance

// Memoize expensive operations
const filtered = useMemo(
  () => items.filter(i => i.active),
  [items]
);