List and Keys in React JS by using map() function


In React JS .map() or map function used to print array element. In normal Javascript you can use a for loop to render or print array elements. On the other hand by using .map() function you can print or render an array easily by typing small amount of code.

Rendering an array in React JS using .map() function

const numbers = [1,2,3,4,5];
const listItems = numbers.map((number) =>
<li key={number.toString()}>{number}</li>
);

ReactDOM.render(
<ul>{listItems}</ul>,
document.getElementByID('root')
);