1

I am trying to add react to a html page without node.

function Hello(){
    return (
        <div>
            <p>Hello World</p>
        </div>
    )
}

ReactDOM.render(<Hello />, document.getElementById('root'))
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>React</title>
</head>
<body>
    <div id="root"></div>
</body>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script type="text/babel" src="main.js"></script>
</html>

main.js is the javascript. Can you help me out with this? Thanks

5
  • Works for me... (but don't use Babel Standalone except for toy snippets you're demonstrating for others, and use createRoot in React 18) Commented Apr 7, 2022 at 2:03
  • Does this help? Maybe the issue is you're not enumerating your presets. stackoverflow.com/questions/40230101/… Commented Apr 7, 2022 at 2:05
  • Are you seeing any errors? Commented Apr 7, 2022 at 2:26
  • @Macilquham No, I don't see any errors in the real version, not stacksnippets Commented Apr 7, 2022 at 2:37
  • If I run your code locally I get a cors error in chrome, if I deploy it to my local iis instance it works perfectly. Commented Apr 7, 2022 at 2:42

3 Answers 3

1

Use ReactDOM.createRoot(rootNode).render(<App />);

Reference- https://17.reactjs.org/docs/concurrent-mode-reference.html#createroot

Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

Maybe if you put the scripts inside the <body> it might work as the elements have to be loaded to be found by the selectors.

For example:

<body>
    <div id="root"></div>

    <!-- Scripts Here -->
</body>

In my browser (Firefox) it works!

3 Comments

thanks, but it doesn't work
Do you see any error in the developer console?
In my browser this works perfectly locally and I don't get any CORS errors! I don't see the problem in the code. It may be an internal react or babel error.
0

Okay, I don't know why this happens but this only happens when the react code is in another file.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>React</title>
</head>
<body>
    <div id="root"></div>
    <div id="ot"></div>
</body>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script type="text/babel">
    var t = 0;
function App(){
  function enter(e){
    e.preventDefault()
    t++
   document.getElementById('ot').innerHTML = `You Clicked The Button ${t} times`
  }
  
  return (
    <div>
    <button onClick={enter}>Click me</button>
    </div>
    )
}

const root = document.getElementById('root');
ReactDOM.render(<App />, root)
</script>
</html>

So that works because for some reason react doesn't work for me in a different file, I have to use inline js. Thank you all for trying to help, and I hope I can help other poeple with this same problem. By the way, there were no errors in the console

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.