Skip to main content

4.05 Outputting Lists

JSX Lists Learning Card PDF

map builds a new array

The map() method creates a new array (of JSX elements) populated with the results of calling a provided function on every element in the calling array. And React will then pull out these elements and render to the screen for you.

App.js
import React, { Component } from "react";
import "./App.css";
import Person from "./Person/Person";

class App extends Component {
state = {
persons: [
{ name: "Max", age: 28 },
{ name: "Manu", age: 29 },
{ name: "Stephanie", age: 26 },
],
};

...

togglePersonsHandler = () => {
const doesShow = this.state.showPersons;
this.setState({ showPersons: !doesShow });
};

render() {

...

let persons = null;

if (this.state.showPersons) {
persons = (
<div>
{this.state.persons.map((person) => {
return <Person name={person.name} age={person.age} />;
})}
</div>
);
}

return (
<div className="App">
<h1>Hi, I'm a React app!</h1>
<p>This is really working!</p>
<button style={style} onClick={this.togglePersonsHandler}>
Toggle Persons
</button>
{persons}
</div>
);
}
}

export default App;
Person.js
const person = (props) => {
return (
<div className="Person">
<p onClick={props.click}>
I'm {props.name} and I am {props.age} years old!
</p>
<p>{props.children}</p>
<input type="text" onChange={props.changed} value={props.name} />
</div>
)
}