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

React Component API

React Component API

The React Component API involves several important aspects, including lifecycle methods, state management, property passing, and event handling.

The following is a detailed description of the React Component API:

Lifecycle methods

The lifecycle methods of React components are divided into three main phases: mounting, updating, and unmounting; see the detailed description:React Component Lifecycle 。

Mounting phase

Example

class

MyComponent

extends

React.

Component

{

  constructor

(

props

)

{

   

super

(

props

)

;

   

this

.

state

=

{

count

:

0

}

;

 

}

 

static

getDerivedStateFromProps

(

nextProps

,

prevState

)

{

   

if

(

nextProps.

reset

)

{

     

return

{

count

:

0

}

;

   

}

   

return

null

;

 

}

  componentDidMount

(

)

{

    console.

log

(

'Component mounted'

)

;

 

}

  render

(

)

{

   

return

<
div
{
this

.

state

.

count

}

div

;

 

}

}

Updating phase

Example

class

MyComponent

extends

React.

Component

{

  shouldComponentUpdate

(

nextProps

,

nextState

)

{

   

return

nextState.

count

!==

this

.

state

.

count

;

 

}

  getSnapshotBeforeUpdate

(

prevProps

,

prevState

)

{

   

return

{

scrollPosition

:

window.

scrollY

}

;

 

}

  componentDidUpdate

(

prevProps

,

prevState

,

snapshot

)

{

   

if

(

snapshot

)

{

      window.

scrollTo

(

0

,

snapshot.

scrollPosition

)

;

   

}

    console.

log

(

'Component updated'

)

;

 

}

  render

(

)

{

   

return

<
div
{
this

.

state

.

count

}

div

;

 

}

}

Unmounting phase

Example

class

MyComponent

extends

React.

Component

{

  componentWillUnmount

(

)

{

    console.

log

(

'Component will unmount'

)

;

 

}

  render

(

)

{

   

return

<
div
{
this

.

state

.

count

}

div

;

 

}

}


State management

State is data internal to a component; use this.state to define and manage it, and update state via the setState method.

Example

class

MyComponent

extends

React.

Component

{

  constructor

(

props

)

{

   

super

(

props

)

;

   

this

.

state

=

{

count

:

0

}

;

 

}

  increment

=

(

)

=>

{

   

this

.

setState

(

(

prevState

)

=>

(

{

count

:

prevState.

count

1

}

)

)

;

 

}

;

  render

(

)

{

   

return

(

     

<
div
<
p
Count

:

{

this

.

state

.

count

}

p

       

<

button onClick

=

{

this

.

increment

}

Increment

button

     

div

   

)

;

 

}

}

Property passing

Access props passed to a component via this.props; you can use PropTypes for type checking.

Example

import

PropTypes from

'prop-types'

;

class

MyComponent

extends

React.

Component

{

  render

(

)

{

   

return

<
div
{
this

.

props

.

title

}

div

;

 

}

}

MyComponent.

propTypes

=

{

  title

:

PropTypes.

string

.

isRequired

,

}

;

// Use a component and pass properties

<

MyComponent title

=

"Hello, World!"

/>

Event Handling

Handle user interactions through event handlers. You need to use .bind(this) or an arrow function to ensure this points correctly.

Example

class

MyComponent

extends

React.

Component

{

  handleClick

=

(

)

=>

{

    console.

log

(

'Button clicked'

)

;

 

}

;

  render

(

)

{

   

return

<

button onClick

=

{

this

.

handleClick

}

Click me

button

;

 

}

}

Conditional Rendering

Control component rendering with conditional statements.

Example

class

MyComponent

extends

React.

Component

{

  constructor

(

props

)

{

   

super

(

props

)

;

   

this

.

state

=

{

isVisible

:

true

}

;

 

}

  toggleVisibility

=

(

)

=>

{

   

this

.

setState

(

(

prevState

)

=>

(

{

isVisible

:

!

prevState.

isVisible

}

)

)

;

 

}

;

  render

(

)

{

   

return

(

     

<
div
{
this

.

state

.

isVisible

&&

<

p

This is visible

p

}

       

<

button onClick

=

{

this

.

toggleVisibility

}

Toggle

button

     

div

   

)

;

 

}

}

List Rendering

Render lists with the map method of arrays.

Example

class

MyComponent

extends

React.

Component

{

  render

(

)

{

   

const

items

=

[

'Item 1'

,

'Item 2'

,

'Item 3'

]

;

   

return

(

     

<
ul
{

items.

map

(

(

item

,

index

)

=>

(

         

<

li key

=

{

index

}

{

item

}

li

       

)

)

}

     

ul

   

)

;

 

}

}

Controlled components

Control the value of form elements through state.

Example

class

MyComponent

extends

React.

Component

{

  constructor

(

props

)

{

   

super

(

props

)

;

   

this

.

state

=

{

value

:

''

}

;

 

}

  handleChange

=

(

event

)

=>

{

   

this

.

setState

(

{

value

:

event.

target

.

value

}

)

;

 

}

;

  handleSubmit

=

(

event

)

=>

{

    event.

preventDefault

(

)

;

    console.

log

(

'Submitted value:'

,

this

.

state

.

value

)

;

 

}

;

  render

(

)

{

   

return

(

     

<

form onSubmit

=

{

this

.

handleSubmit

}

       

<

input type

=

"text"

value

=

{

this

.

state

.

value

}

onChange

=

{

this

.

handleChange

}

/>

       

<

button type

=

"submit"

Submit

button

     

form

   

)

;

 

}

}


Setting state: setState

setState is the method used in React to update a component's state.

The syntax format is as follows:


setState(object nextState[, function callback])

Parameter Description

Merge nextState with the current state and re-render the component.

setState is the primary method for triggering UI updates in React event handlers and request callbacks.

About setState

You cannot modify state via this.state inside a component, because that state will be replaced after calling setState().

setState() does not immediately change this.state; instead, it creates a state that is about to be processed. setState() is not necessarily synchronous; to improve performance, React batches state updates and DOM rendering.

setState() always triggers a component re-render, unless some conditional rendering logic is implemented in shouldComponentUpdate().

By using setState reasonably, you can effectively manage component state and ensure necessary operations are performed after state updates, thereby improving the responsiveness and reliability of the application.

React example

class

Counter

extends

React

.

Component

{

constructor

(

props

)

{

super

(

props

)

;

this

.

state

=

{

clickCount

:

0

}

;

this

.

handleClick

=

this

.

handleClick

.

bind

(

this

)

;

}

handleClick

(

)

{

this

.

setState

(

function

(

state

)

{

return

{

clickCount

:

state

.

clickCount

1

}

;

}

)

;

}

render

(

)

{

return

(

<
h2
onClick
=
{
this

.

handleClick

}

Click me! Click count:

{

this

.

state

.

clickCount

}

h2

)

;

}

}

const

root

=

ReactDOM

.

createRoot

(

document

.

getElementById

(

"

root

"

)

)

;

root

.

render

(

<
Counter
/>
)

;

Try it »

In the example, clicking the h2 tag increments the click counter by 1.