React componentDidUpdate() Method
The componentDidUpdate() method has the following format:
componentDidUpdate(prevProps, prevState, snapshot)
The componentDidUpdate() method is called immediately after the component updates.
This method is not executed on the initial render.
You can also call setState() directly in componentDidUpdate(), but note that it must be wrapped in a conditional statement.
The following example uses the componentDidUpdate() method to execute after the component updates; the component using the componentDidMount() method will 1 make a modification after a few seconds:
Example
class
Header
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
)
;
this
.
state
=
{
favoritesite
:
"
runoob
"
}
;
}
componentDidMount
(
)
{
setTimeout
(
(
)
=>
{
this
.
setState
(
{
favoritesite
:
"
"
}
)
}
,
1000
)
}
componentDidUpdate
(
)
{
document
.
getElementById
(
"
mydiv
"
)
.
innerHTML
=
"
After updating, the favorite is
"
this
.
state
.
favoritesite
;
}
render
(
)
{
return
(
<
div
<
h1My favorite website is
{
this
.
state
.
favoritesite
}
h1
< div id = " mydiv " div div );
}
}
Try it »
React Component Lifecycle