Skip to main content

7.07 Component Creation Lifecycle In Action

App.js

App.js
import React, { Component } from "react";
import Cockpit from "../components/Cockpit/Cockpit";
import Persons from "../components/Persons/Persons";
import classes from "./App.css";
class App extends Component {
constructor(props) {
super(props);
console.log("[App.js] constructor");
}
state = {
persons: [
{ id: "20210330005200", name: "Max", age: 28 },
{ id: "20210330005205", name: "Manu", age: 29 },
{ id: "20210330005209", name: "Stephanie", age: 26 },
],
};

static getDerivedStateFromProps(props, state) {
console.log("[App.js] getDerivedStateFromProps", props);
return state;
}

// componentWillMount() {
// console.log("[App.js] componentWillMount");
// }
componentDidMount() {
console.log("[App.js] componentDidMount");
}
nameChangedHandler = (event, id) => {
const personIndex = this.state.persons.findIndex((p) => {
return p.id === id;
});

const person = {
...this.state.persons[personIndex],
};

// const person = Object.assign({}, this.state.persons[personIndex]);

person.name = event.target.value;

const persons = [...this.state.persons];
persons[personIndex] = person;

this.setState({
persons: persons,
});
};

deletePersonHandler = (personIndex) => {
// const persons = this.state.persons.slice();
const persons = [...this.state.persons];
persons.splice(personIndex, 1);
this.setState({ persons: persons });
};

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

render() {
console.log("[App.js] render");
let persons = null;

if (this.state.showPersons) {
persons = (
<Persons
persons={this.state.persons}
clicked={this.deletePersonHandler}
changed={this.nameChangedHandler}
/>
);
}

return (
<div className={classes.App}>
<Cockpit
title={this.props.appTitle}
persons={this.state.persons}
showPersons={this.state.showPersons}
clicked={this.togglePersonsHandler}
/>
{persons}
</div>
);
}
}

export default App;

Person.js

Person.js
import React from "react";
import classes from "./Person.css";

const person = (props) => {
console.log("[Person.js] rendering...");
return (
<div className={classes.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>
);
};

export default person;

Persons.js

App.js
import React from "react";
import Person from "./Person/Person";

const persons = (props) => {
console.log("[Persons.js] rendering...");
return props.persons.map((person, index) => {
return (
<Person
click={() => props.clicked(index)}
name={person.name}
age={person.age}
key={person.id}
changed={(event) => props.changed(event, person.id)}
/>
);
});
};
export default persons;