Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Getting Started with React | Getting Started: JSX
Introduction to React

book
Getting Started with React

The simplest way to start using React is to create a basic HTML page:

html

index.html

copy
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>React Application</title>
</head>
<body>
<!-- React code will go here -->
</body>
</html>

Afterward, to install React, we will import React to our project by adding the following links in the header section:

html

index.html

copy
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script
src="https://unpkg.com/react@18/umd/react.production.min.js"
crossorigin >
</script>
<script
src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"
crossorigin >
</script>
<title>React Application</title>
</head>
<body>
<!-- React code will go here -->
</body>
</html>

However, this won't be sufficient since we also want to be able to use JSX for ease in creating elements. After adding all three of these links, the HTML script should look something like this:

html

index.html

copy
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>React Application</title>
<!-- React Import -->
<script
src="https://unpkg.com/react@18/umd/react.production.min.js"
crossorigin >
</script>
<script
src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"
crossorigin >
</script>
<!-- JSX Import -->
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
</head>
<body>
<!-- React code will go here -->
</body>
</html>

React needs an HTML container element (for example, a <div> element) to render components inside the HTML document. This base container is also often referred to as root.

Everything React renders will be inside this root element. We will create a <div> element in the HTML body, with an id "root" so we can access it from JavaScript code.

html

index.html

copy
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>React Application</title>
<!-- React Import -->
<script
src="https://unpkg.com/react@18/umd/react.production.min.js"
crossorigin >
</script>
<script
src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"
crossorigin >
</script>
<!-- JSX Import -->
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
// React code
</script>
</body>
</html>

To complete the program, we will add two additional lines of code, which will print a "Hello World!" heading inside the root:

html

index.html

copy
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>React Application</title>
<!-- React Import -->
<script
src="https://unpkg.com/react@18/umd/react.production.min.js"
crossorigin >
</script>
<script
src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"
crossorigin >
</script>
<!-- JSX Import -->
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<h1>Hello, world!</h1>);
</script>
</body>
</html>

The ReactDOM.createRoot function creates a React root object from the referenced element – an object that enables React to create elements inside the HTML document. Here the referenced element is the <div> having the id="root" (document.getElementById('root')).

The second line of code is the render method of the root object. The render method accepts JSX elements or React components to be rendered inside the root. Here we are rendering a simple <h1> element having "Hello World!" as its content.

question mark

What is a React root?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 5
some-alt