React lifecycle(1)
React lifecycle(1)
Let’s look at a simple example. If I told you to build a Hello World component,
you might write something like this:
import React, { Component } from 'react'
export default class App extends Component {
render() {
return (
<div>
<h1>HelloWorld</h1>
</div>
)
}
}
The constructor() method is called before anything else, when the component
is initiated, and it is the natural place to set up the initial state and other initial
values.
The constructor() method is called with the props, as arguments, and you should
always start by calling the super(props) before anything else, this will initiate the
parent's constructor method and allows the component to inherit methods from
its parent (React Component).
import React, { Component } from 'react';
export default class App extends Component {
constructor(props){
super(props);
this.state = {color: "red"};
}
render() {
return (
<div>
<h1>My Favorite Color is {this.state.color}</h1>
</div>
)
}
}
Output:
getDerivedStateFromProps
The getDerivedStateFromProps() method is called right before rendering
the element(s) in the DOM.
This is the natural place to set the state object based on the initial props.
It takes state as an argument, and returns an object with changes to the
state.
The example below starts with the favorite color being "red", but the
getDerivedStateFromProps() method updates the favorite color based on the
favcol attribute:
render()
After the static getDerivedStateFromProps method is called, the next lifecycle
method in line is the render method:
The render() method is required, and is the method that actually outputs
the HTML to the DOM.
componentDidMount
The componentDidMount() method is called after the component is rendered.
This is where you run statements that requires that the component is already
placed in the DOM.
Updating
The next phase in the lifecycle is when a component is updated.
React has five built-in methods that gets called, in this order, when a
component is updated:
1. getDerivedStateFromProps()
2. shouldComponentUpdate()
3. render()
4. getSnapshotBeforeUpdate()
5. componentDidUpdate()
The render() method is required and will always be called, the others are
optional and will be called if you define them.
getDerivedStateFromProps
Also at updates the getDerivedStateFromProps method is called. This is
the first method that is called when a component gets updated.
This is still the natural place to set the state object based on the initial
props.
The example below has a button that changes the favorite color to blue, but
since the getDerivedStateFromProps() method is called, which updates
the state with the color from the favcol attribute, the favorite color is still
rendered as yellow:
shouldComponentUpdate
In the shouldComponentUpdate() method you can return a Boolean value that
specifies whether React should continue with the rendering or not.
render
The render() method is of course called when a component gets updated, it
has to re-render the HTML to the DOM, with the new changes.
The example below has a button that changes the favorite color to blue:
getSnapshotBeforeUpdate
In the getSnapshotBeforeUpdate() method you have access to
the props and state before the update, meaning that even after the update,
you can check what the values were before the update.
The example below might seem complicated, but all it does is this:
When the component is mounting it is rendered with the favorite color "red".
When the component has been mounted, a timer changes the state, and
after one second, the favorite color becomes "yellow".
This action triggers the update phase, and since this component has
a getSnapshotBeforeUpdate() method, this method is executed, and writes a
message to the empty DIV1 element.
componentDidUpdate
The componentDidUpdate method is called after the component is updated in
the DOM.
Unmounting
The next phase in the lifecycle is when a component is removed from the
DOM, or unmounting as React likes to call it.
React has only one built-in method that gets called when a component is
unmounted:
componentWillUnmount()
componentWillUnmount
The componentWillUnmount method is called when the component is about
to be removed from the DOM.