HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

React Lists

React Lists

In React, working with lists involves rendering a collection of elements, such as an array of data, and displaying them in the user interface.

React provides a simple and efficient way to handle lists using the 'map' function.

Here's a basic example:

Suppose you have an array of items:

const items = ["Item 1", "Item 2", "Item 3"];

You can render a list of these items in a React component like this:

import React from 'react';

const MyListComponent = () => {
const items = ["Item 1", "Item 2", "Item 3"];

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

export default MyListComponent;

A few key points:

  • The 'map' function is used to iterate over each item in the array.
  • Each item is assigned a 'key' prop, which helps React identify each list item uniquely. This is important for efficient rendering and updating of the component.
  • The JSX code inside the 'map' function renders a list item ('<li>') for each item in the array.