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

React Element Rendering

React Element Rendering

In React 18, element rendering has some changes compared to previous versions, especially regarding the use of ReactDOM.createRoot and the new concurrent features. Below is a detailed example and explanation showing how to render elements in React 18.

Example

import

React from

'react'

;

import

ReactDOM from

'react-dom/client'

;

// Create a simple React component

function

App

(

)

{

   

return

<
h1
Hello
,
React
18

!

h1

;

}

// Get the DOM container with id "example" and create a React root node

const

root

=

ReactDOM.

createRoot

(

document.

getElementById

(

"example"

)

)

;

// Render the React component to the root node in the DOM

root.

render

(

<
App
/>
)

;

React elements are the smallest units for building React applications; they describe what you want to see on the screen. React elements are immutable objects and cannot be changed once created.

const element = 

Hello, world!

;

Unlike browser DOM elements, elements in React are actually plain objects, and React DOM ensures that the browser DOM's data content stays consistent with the React elements.

Rendering Elements into the DOM

First, we add an element with id="example" to an HTML page:

:

Everything inside this div will be managed by React DOM, so we call it the "root" DOM node.

When developing applications with React, we generally define only one root node. But if you are introducing React into an existing project, you may need to define React root nodes separately in different parts.

To render React elements into the root DOM node, we pass them all to the root.render() method to render them onto the page:

Example

//

Create a React element

const

element

=<

h1

Hello

,

world

!

h1

;

//

Get the DOM container and create the root node

const

root

=

ReactDOM

.

createRoot

(

document

.

getElementById

(

"

example

"

)

)

;

//

Render the React element to the root node in the DOM

root

.

render

(

element

)

;

Try it »

The root.render method renders the element into the previously created root node, so the element will be displayed inside the DOM element with id "example".

Updating Element Rendering

React elements are immutable. Once an element is created, you cannot change its content or properties.

Currently the only way to update the interface is to create a new element and pass it to the ReactDOM.render() method:

Let's look at this timer example:

Example

const

root

=

ReactDOM

.

createRoot

(

document

.

getElementById

(

"

example

"

)

)

;

function

tick

(

)

{

const

element

=

(

<
div
<
h1
Hello
,
world

!

h1

<

h2

The time is now

{

new

Date

(

)

.

toLocaleTimeString

(

)

}

.

h2

div

)

;

root

.

render

(

element

)

;

}

setInterval

(

tick

,

1000

)

;

Try it »

The above example calls ReactDOM.render() once per second using the setInterval() method.

We can encapsulate the part to be displayed; the following example uses a function to represent it:

Example

function

Clock

(

props

)

{

return

(

<
div
<
h1
Hello
,
world

!

h1

<

h2

The time is now

{

props

.

date

.

toLocaleTimeString

(

)

}

.

h2

div

)

;

}

function

tick

(

)

{

const

root

=

ReactDOM

.

createRoot

(

document

.

getElementById

(

"

example

"

)

)

;

root

.

render

(

<
Clock
date
=
{
new
Date
(
)
}
/>
)

;

}

setInterval

(

tick

,

1

)

;

Try it »

Besides functions, we can also create an ES6 class extending React.Component that encapsulates the elements to be displayed. Note that inside the render() method, you need to use this.props instead of props:

Example

class

Clock

extends

React

.

Component

{

render

(

)

{

return

(

<
div
<
h1
Hello
,
world

!

h1

<

h2

The time is now

{

this

.

props

.

date

.

toLocaleTimeString

(

)

}

.

h2

div

)

;

}

}

function

tick

(

)

{

const

root

=

ReactDOM

.

createRoot

(

document

.

getElementById

(

"

example

"

)

)

;

root

.

render

(

<
Clock
date
=
{
new
Date
(
)
}
/>
)

;

}

setInterval

(

tick

,

1

)

;

Try it »

React Only Updates What Is Necessary

It is worth noting that React DOM first compares the differences in element content before and after, and only updates the parts that have changed during rendering.