Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Getting familiar with JSX | React in Practice
Introduction to React

book
Getting familiar with JSX

If you recall from the JSX chapters, we can store HTML elements inside JavaScript variables. Let's try storing a simple heading element inside a variable with an embedded name value:

js
let name = "Mouiz G";
let heading = <h1>Hello, {name}!</h1>

After adding these two lines in the script, we will be able to render the element heading by passing it into the render method of the root instead of the older value:root.render(element);

If you look in the output console, you should be able to see rendered results.

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">
let name = "Mouiz G";
let heading = <h1>Hello, {name}!</h1>;
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(heading);
</script>
</body>
</html>

This task isn't obligatory to complete the course!

Task

On Line 26, create an unordered list having three list items Item 1, Item 2, and Item 3. Store it in a variable called myList. Render it using the root's render function.

Note

You can use the codesandbox window below. Sliding from the right edge will reveal the result (rendered page).

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 1

Vraag AI

expand
ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

We use cookies to make your experience better!
some-alt