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:
- react.min.js - The core library of React
- react-dom.min.js - Provides DOM-related functionality
- babel.min.js - Babel can convert ES6 code to ES5 code, so that we can run React code in browsers that do not currently support ES6. Babel has built-in support for JSX. Using Babel together with the babel-sublime package can take the syntax rendering of source code to a whole new level.
//
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"));
- Getting the DOM container:
document.getElementById("example")Gets a DOM element with the id "example". - Creating the root node:
ReactDOM.createRootis a new method introduced in React 18, used to create a React root node. In this example,rootthe variable holds this root node.
Note:
If we need to use JSX, then