react for each loop code example
Example 1: jsx foreach
render() {
const myArray = [];
return <>
{myArray.map(item => {
return (<Element>
{item}
</Element>);
});}
</>
}
Example 2: react for loop in render
render: function() {
const elements = ['one', 'two', 'three'];
return (
<ul>
{elements.map((value, index) => {
return <li key={index}>{value}</li>
})}
</ul>
)
}
Example 3: react loop through array
this.items = this.state.cart.map((item, key) => <li key={item.id}>{item.name}</li>);