Skip to content

Instantly share code, notes, and snippets.

@debonx
Created January 4, 2020 11:51
Show Gist options
  • Save debonx/88ec7e1ed24f58c1f88262f286e3a300 to your computer and use it in GitHub Desktop.
Save debonx/88ec7e1ed24f58c1f88262f286e3a300 to your computer and use it in GitHub Desktop.
React: Updating and Unmounting lifecycle methods.

Updating Lifecycle Methods

What is updating?

The first time that a component instance renders, it does not update. A component updates every time that it renders, starting with the second render. There are five updating lifecycle methods:

  • componentWillReceiveProps
  • shouldComponentUpdate
  • componentWillUpdate
  • render
  • componentDidUpdate

Whenever a component instance updates, it automatically calls all five of these methods, in order.

Unmounting Lifecycle Methods

A component’s unmounting period occurs when the component is removed from the DOM. This could happen if the DOM is rerendered without the component, or if the user navigates to a different website or closes their web browser.

  • componentWillUnmount is the only unmounting lifecycle method!

componentWillUnmount gets called right before a component is removed from the DOM. If a component initiates any methods that require cleanup, then componentWillUnmount is where putting that cleanup.

import React from 'react';
export class Example extends React.component {
componentDidUpdate(prevProps, prevState) {
alert('Component is done rendering!');
}
render() {
return <h1>Hello world</h1>;
}
}
import React from 'react';
export class Example extends React.Component {
componentWillReceiveProps(nextProps) {
alert("Check out the new props.text that "
+ "I'm about to get: " + nextProps.text);
}
render() {
return <h1>{this.props.text}</h1>;
}
}
// The first render won't trigger
// componentWillReceiveProps:
ReactDOM.render(
<Example text="Hello world" />,
document.getElementById('app')
);
// After the first render,
// subsequent renders will trigger
// componentWillReceiveProps:
setTimeout(() => {
ReactDOM.render(
<Example text="Hello world" />,
document.getElementById('app')
);
}, 1000);
import React from 'react';
export class Example extends React.Component {
componentWillUnmount() {
alert('Goodbye world');
}
render() {
return <h1>Hello world</h1>;
}
}
import React from 'react';
export class Example extends React.Component {
componentWillUpdate(nextProps, nextState) {
alert('Component is about to update! Any second now!');
}
render() {
return <h1>Hello world</h1>;
}
}
import React from 'react';
export class Example extends React.Component {
constructor(props) {
super(props);
this.state = { subtext: 'Put me in an <h2> please.' };
}
shouldComponentUpdate(nextProps, nextState) {
if ((this.props.text == nextProps.text) &&
(this.state.subtext == nextState.subtext)) {
alert("Props and state haven't changed, so I'm not gonna update!");
return false;
} else {
alert("Okay fine I will update.")
return true;
}
}
render() {
return (
<div>
<h1>{this.props.text}</h1>
<h2>{this.state.subtext}</h2>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment