ReactDOM.render()

Ayu is a tech blogger, open source contributor & maintainer, and tech community enthusiast based in the Netherlands. She loves photography and iced coffee!
Search for a command to run...

Ayu is a tech blogger, open source contributor & maintainer, and tech community enthusiast based in the Netherlands. She loves photography and iced coffee!
Thank you, Rahul 😊
Hi friends 👋, I've been writing tech blog posts for a few years now. If you don't know me yet, English is my second language. My mother tongue is Indonesian, and I'm also surrounded by Dutch every da

I frequently receive messages from enthusiastic contributors who have completed 2 or 3 high-quality pull requests (PRs) in just 1 month. They’re proactive, their work is top-tier, and they’ve handled

A few months ago, I shared a story about building an automated open source portfolio using just my smartphone and an AI assistant while on vacation. My main goal was to stop the "spreadsheet struggle"

Recently, I read Bekah Hawrot Weigel’s blog post, "When 'Local' Went Global: The Pandemic Era of International Communities." As someone active in many online communities, I found it a bit sad, but I agree with her points and think they apply to open ...

Hacktoberfest is almost over. How has your contribution journey been so far? Are you confident about contributing to open source projects, or are you still struggling to find your way? If you're new to open source and still feel overwhelmed, that's c...

Hello Fellow Codenewbies 👋
One functionality of ReactDOM is to render React elements to the web page, which can be achieved with ReactDOM.render() method.
ReactDOM.render(element, container[, callback])
We can see it this way:
ReactDOM.render(WHAT to render, WHERE to render[, callback if any])
Before we dive in, in this post, we will use both React, and ReactDOM CDN links in an HTML file to run the React app instead of installing the create-react-app. This way, you can see and understand how react-dom dependency works under the hood.
<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
We also need Babel to convert the JSX syntax.
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
ReactDOM.render() In ActionLet's create a <div> with id="root" in an index.html file.
<html>
<head>
...
</head>
<body>
<div id="root"></div>
<!-- CDN Links -->
...
</body>
</html>
Anything between the opening and the closing div tag is where React will render what we've created.
The <div id="root"></div> is the container for our entire application.
Now we will render an h1.
Since we need Babel to translate the JSX, we will run the ReactDOM.render() in <script type="text/babel">.
<script type="text/babel">
ReactDOM.render(<h1>Hello World</h1>, document.getElementById("root"))
</script>
📖 How to read:
Render h1 in the <div> with id="root".
As you can see, we put the HTML element of h1 in the middle of Javascript's code as a parameter. That entire line of code is called JSX.
We will talk more about JSX in the next post.
ReactDOM.render() is a React method to render a React app to the web page.ReactDOM.render() takes at least 2 parameters:I provide here the complete code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ReactDOM.render()</title>
</head>
<body>
<div id="root"></div>
<!-- Babel CDN Link -->
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<!-- React & ReactDOM CDN Links -->
<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<!-- Render to page -->
<script type="text/babel">
ReactDOM.render(<h1>Hello World</h1>, document.getElementById("root"))
</script>
</body>
</html>