2. Rendering lists #

Created Wednesday 09 September 2020

Why #

React can render a hierarchied JSX object. But what about rendering sibling nodes. React can render them too.

How #

React render an array of JSX objects as sibling nodes. Example

return <div>{[<span> Hello </span>, <span> World </span>]}</div>
return <div>{names.map( (name) => <div>{name}</div> )} </div>} // commonly done

Note: React can render sibling nodes, but a single outermost container is still needed.

What #

How to output/render multiple components together on a page? Some approaches are:

A simple way is to use map in JS, and this is used quite a lot. e.g Making a list of components and, then rendering it

const ninjaList = ninjas.map(
	ninja => (<div>
				<div>Name: {ninja.name} </div>
				<div>Age: {ninja.age} </div>
				<div>Belt: {ninja.belt} </div>
			</div>));
return (<div> {ninjaList} </div>);	// render function

Key attribute #

Conditions for key #