Home > Article > Web Front-end > How to Efficiently Render Arrays in React?
React Array Rendering Made Easy
Rendering a list of elements in React is a common task. Here's how to do it efficiently:
First Method:
<code class="javascript">render() { const data =[{"name":"test1"},{"name":"test2"}]; const listItems = data.map((d) => <li key={d.name}>{d.name}</li>; return ( <div>{listItems}</div> ); }</code>
This method involves creating an array of React elements and then rendering them in a container div.
Second Method:
<code class="javascript">render() { const data =[{"name":"test1"},{"name":"test2"}]; return ( <div> {data.map(function(d, idx){ return (<li key={idx}>{d.name}</li>) })} </div> ); }</code>
This method uses the map function directly in the return statement, which simplifies the syntax.
Either of these methods can be used to render arrays of objects in React, depending on your preference.
The above is the detailed content of How to Efficiently Render Arrays in React?. For more information, please follow other related articles on the PHP Chinese website!