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

React Installation (CDN)

React Installation (CDN)

React can be downloaded and used directly, and the download package also provides many learning examples.

This tutorial uses React version 18.2.0; you can visit the official website https://reactjs.org/ to download the latest version.

To use ByteDance's React CDN library, the addresses are as follows:

To use the Staticfile CDN React library, the addresses are as follows:

<

script

src

=

"

https://cdn.staticfile.org/react/18.2.0/umd/react.development.js

"

script

<

script

src

=

"

https://cdn.staticfile.org/react-dom/18.2.0/umd/react-dom.development.js

"

script

<

script

src

=

"

https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js

"

script

Officially provided CDN addresses:

<

script

src

=

"

https://unpkg.com/react@16/umd/react.development.js

"

script

<

script

src

=

"

https://unpkg.com/react-dom@16/umd/react-dom.development.js

"

script

<

script

src

=

"

https://unpkg.com/babel-standalone@6.15.0/babel.min.js

"

script

Note: Using Babel in the browser to compile JSX is very inefficient.

Usage Example

The following example outputs Hello, React!

React example

<!DOCTYPE html>

<

html

<

head

<

meta

charset

=

"

UTF-8

"

/>

<

title

Hello React!

title

<

script

src

=

"

https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js

"

script

<

script

src

=

"

https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js

"

script

<

script

src

=

"

https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js

"

script

head

<

body

<

div

id

=

"

example

"

div

<

script

type

=

"

text/babel

"

// Simple React component function App() { return

<

h1

Hello, React!

h1

; } const root = ReactDOM.createRoot(document.getElementById("example")); // render the React component to the DOM root.render(

<

App

/>

);

script

body

html

script

body

html

Try it »

Example analysis:

In this example we have included three libraries: react.production.min.js, react-dom.production.min.js and babel.min.js:

//

A simple React component

function

App

(

)

{

return

<

h1

Hello

,

React

!

h1

;

}

const

root

=

ReactDOM

.

createRoot

(

document

.

getElementById

(

"

example

"

)

)

;

//

Rendering a React component to the DOM

root

.

render

(

<

App

/>

)

;

The above code inserts an h1 heading into the node with id="example".

const root = ReactDOM.createRoot(document.getElementById("example"));

Note:

If we need to use JSX, then