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

React Component Lifecycle

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

When a component instance is created and inserted into the DOM, its lifecycle methods are called in the following order:

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:

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:

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 »