<!DOCTYPE html>
<html lang="en">
<head>
<title>React App</title>
</head>
<body>
<script src="../src/index.js" type="text/JSX"></script>
<div id="root"></div>
</body>
</html>
import React from 'react';
import ReactDOM from 'react-dom/client';
ReactDOM.render(<h1>Hello World!</h1>,document.getElementById('root'));
When exececuting this code ,Instead of ‘Hello World!’, all i am getting is a blank Page.Some one please explain why?
Got a blank page instead of "hello world!"
2
Answers
It doesn’t appear that you’re using React properly. JSX need to be transpiled into Javascript for the browser to understand it. You’ll need Babel for that. Also, React needs to be in scope to be able to import it.
If you just want to try React in a simple way locally (not for production), you can use this template:
Otherwise, follow the installation instructions in the official documentation, or use create-react-app to properly bootstrap your application.
The issue is with the way you are importing
ReactDOM
. Therender()
function is not available inside thereact-dom/client
. Changing it to import fromreact-dom
would do the trickSo the file would look like this :