Approach 4 Using React.Children.map()

  1. It is a utility function in React
  2. It is used to iterate over the child component
  3. It takes a callback function as an argument
  4. The callback functions takes child and index as arguments to iterate over the child components and modify it

Syntax:

React.Children.map(children, (child, index) => (
// write your code here
))

Example: Below is the example of using React.Children.map():

Javascript
import React from 'react';

const arrayList = ['List Item 1', 'List Item 2', 'List Item 3'];

const UsingReactChildrenMap = ({ children }) => {
    const modifiedChildren =
        React.Children.map(children, (child, index) => (
            <div key={index}>Modified {child}</div>
        ));
    return <div>{modifiedChildren}</div>;
};

const App = () => (
    <div>
        <h2>
            Using React.Children.map()
        </h2>
        <UsingReactChildrenMap>
            <div style={
                {
                    display: 'inline-block',
                    marginRight: '10px'
                }}>
                {arrayList[0]}
            </div>
            <div style={
                {
                    display: 'inline-block',
                    marginRight: '10px'
                }}>
                {arrayList[1]}
            </div>
            <div style={
                {
                    display: 'inline-block',
                    marginRight: '10px'
                }}>
                {arrayList[2]}
            </div>
        </UsingReactChildrenMap>
    </div>
);

export default App;

Output:

OUTPUT IMAGE – React.Children.map() Method

Different ways to render a list of items in React

This article explains the various approaches for rendering a list of items in React. Render List means displaying various items present in the list. It is a very commonly used operation in applications. React can help in the dynamic rendering of list items.

We will discuss the following approaches to render a list of items in React:

Table of Content

  • Using Array.map()
  • Using for loop
  • Using Array.forEach()
  • Using React.Children.map()
  • Using map() and JSX Spread Attributes

Similar Reads

Approach 1: Using Array.map():

It is an inbuilt javascript functionIt iterates over list of items in array and returns a list of React componentsmap() function called over the array and a callback function is accepted as an argumentcallback function will take item and index as argumentindex should be used a key of the item...

Approach 2: Using for loop:

for loop is a control flow statement in JavascriptIt is used to iterate over the items in a arrayIn this iterator or loop variable will be used to access the array elements...

Approach 3: Using Array.forEach():

forEach() function takes a callback function as an argumentThat callback function will be executed for each array itemcallback function takes item and index as argument...

Approach 4: Using React.Children.map():

It is a utility function in ReactIt is used to iterate over the child componentIt takes a callback function as an argumentThe callback functions takes child and index as arguments to iterate over the child components and modify it...

Approach 5: Using map() and JSX Spread Attributes:

This method is used to render the components with dynamic propsIn the callback function while rendering the components props will be passed to itThe callback function take item and index as the argument...

Contact Us