Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

React componentDidUpdate() Method

React componentDidUpdate() Method

React Component Lifecycle React Component Lifecycle

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

:

"

google

"

}

)

}

,

1000

)

}

componentDidUpdate

(

)

{

document

.

getElementById

(

"

mydiv

"

)

.

innerHTML

=

"

After updating, the favorite is

"

this

.

state

.

favoritesite

;

}

render

(

)

{

return

(

<
div
<
h1

My favorite website is

{

this

.

state

.

favoritesite

}

h1

<
div
id
=
"
mydiv
"
div
div
)

;

}

}

Try it »

React Component Lifecycle React Component Lifecycle