Skip to main content

7.12 Cleaning Up With Lifecycle Hooks And useEffect

Person.js
...
}

componentWillUnmount() {
console.log("[Persons.js] componentWillUnmount");
}

render() {
...
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 },
],
showCockpit: true,
};

...

return (
<div className={classes.App}>
<button
onClick={() => {
this.setState({ showCockpit: !this.state.showCockpit });
}}
>
Remove Cockpit
</button>
{this.state.showCockpit ? (
<Cockpit
title={this.props.appTitle}
persons={this.state.persons}
showPersons={this.state.showPersons}
clicked={this.togglePersonsHandler}
/>
) : null}
{persons}
</div>
);
}
}

export default App;

useEffect()

It runs before the main useEffect function runs, but after the (first) render cycle!

If you passed an empty array, useEffect will basically execute this function only, this function here, only when that component rendered and when it is unmounted.

Cockpit.js
import React, { useEffect } from "react";
import classes from "./Cockpit.css";

const cockpit = (props) => {
useEffect(() => {
console.log("[Cockpit.js] useEffect");
// Http request...
setTimeout(() => {
alert("Saved data to cloud!");
}, 1000);
return () => {
console.log("[Cockpit.js] cleanup work in useEffect");
};
}, []);

//}, [props.persons]);

// useEffect()

useEffect(() => {
console.log("[Cockpit.js] 2nd useEffect");
return () => {
console.log("[Cockpit.js] cleanup work in 2nd useEffect");
};
});

const assignedClasses = [];
let btnClass = "";
if (props.showPersons) btnClass = classes.Red;
if (props.persons.length <= 2) {
assignedClasses.push(classes.red); // classes = ['red']
}
if (props.persons.length <= 1) {
assignedClasses.push(classes.bold); // classes = ['red', 'bold']
}
return (
<div className={classes.Cockpit}>
<h1>{props.title}</h1>
<p className={assignedClasses.join(" ")}>This is really working!</p>
<button className={btnClass} onClick={props.clicked}>
Toggle Persons
</button>
</div>
);
};

export default cockpit;