React Component Lifecycle
In this chapter we will discuss the lifecycle of React components.
A component's lifecycle can be divided into three states:
- Mounting: inserted into the real DOM
- Updating: being re-rendered
- Unmounting: removed from the real DOM

mounting
When a component instance is created and inserted into the DOM, its lifecycle methods are called in the following order:
constructor(): Before a React component mounts, its constructor is called.getDerivedStateFromProps(): Called before the render method, and invoked both on initial mount and on subsequent updates.render(): The render() method is the only method that must be implemented in a class component.componentDidMount(): Called immediately after the component mounts (is inserted into the DOM tree).
The render() method is the only method that must be implemented in a class component; other methods can be implemented as needed.
For detailed descriptions of these methods, seeOfficial documentation。
update
Whenever a component's state or props change, the component updates.
An update is triggered when a component's props or state change. The lifecycle methods during an update are called in the following order:
getDerivedStateFromProps(): Called before the render method, and invoked both on initial mount and on subsequent updates. Based on the return value of shouldComponentUpdate(), it determines whether the React component's output is affected by the current state or props changes.shouldComponentUpdate(): When props or state change, shouldComponentUpdate() is called before rendering is performed.render(): The render() method is the only method that must be implemented in a class component.getSnapshotBeforeUpdate(): Called just before the most recent render output is committed to the DOM nodes.componentDidUpdate(): Called immediately after an update.
The render() method is the only method that must be implemented in a class component; other methods can be implemented as needed.
For detailed descriptions of these methods, seeOfficial documentation。
Uninstall
When a component is removed from the DOM, the following method is called:
componentWillUnmount(): Called directly before the component is unmounted and destroyed.
For detailed descriptions of these methods, seeOfficial documentation。
Example
The following is an example of the current time, updated every second:
Example
class
Clock
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
)
;
this
.
state
=
{
date
:
new
Date
(
)
}
;
}
componentDidMount
(
)
{
this
.
timerID
=
setInterval
(
(
)
=>
this
.
tick
(
)
,
1000
)
;
}
componentWillUnmount
(
)
{
clearInterval
(
this
.
timerID
)
;
}
tick
(
)
{
this
.
setState
(
{
date
:
new
Date
(
)
}
)
;
}
render
(
)
{
return
(
<div>
<h1 Hello,Runoob>!
h1
<
h2
The current time is:
{
this
.
state
.
date
.
toLocaleTimeString
(
)
}
.
h2
div
)
;
}
}
const
root
=
ReactDOM
.
createRoot
(
document
.
body
)
;
root
.
render
(
<
Clock
/>
);
Try it »
In the following example, after the Hello component loads, a timer is set via the componentDidMount method to reset the component's opacity every 100 milliseconds and re-render:
React example
class
Hello
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
)
;
this
.
state
=
{
opacity
:
1.0
}
;
}
componentDidMount
(
)
{
this
.
timer
=
setInterval
(
function
(
)
{
var
opacity
=
this
.
state
.
opacity
;
opacity
-=
.05
;
if
(
opacity
<
0.1
)
{
opacity
=
1.0;
}
this
.
setState
(
{
opacity
:
opacity
}
)
;
}
.
bind
(
this
)
,
100
)
;
}
render
(
)
{
return
(
<div style={{opacity>:
this
.
state
.
opacity
}
}
Hello
{
this
.
props
.
name
}
div
)
;
}
}
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
<
Hello
name
=
"
world
"
/>
);
Try it »
The following example initializes state , setNewnumber used to update state. All lifecycles are in Content components.
React example
class
Button
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
)
;
this
.
state
=
{
data
:
0
}
;
this
.
setNewNumber
=
this
.
setNewNumber
.
bind
(
this
)
;
}
setNewNumber
(
)
{
this
.
setState
(
{
data
:
this
.
state
.
data
1
}
)
;
}
render
(
)
{
return
(
<div>
<button onClick={this>.
setNewNumber
}
INCREMENT
button
< Content myNumber = { this.
state
.
data
}
/>
div
)
;
}
}
class
Content
extends
React
.
Component
{
componentDidMount
(
)
{
console
.
log
(
"
Component DID MOUNT!
"
)
;
}
shouldComponentUpdate
(
newProps
,
newState
)
{
return
true
;
}
componentDidUpdate
(
prevProps
,
prevState
)
{
console
.
log
(
"
Component DID UPDATE!
"
)
;
}
componentWillUnmount
(
)
{
console
.
log
(
"
Component WILL UNMOUNT!
"
)
;
}
render
(
)
{
return
(
<div> <h3{this>.
props
.
myNumber
}
h3
div
)
;
}
}
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
"
root
"
)
)
;
root
.
render
(
< div < Button/>
div
)
;
Try it »