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

  • Works for me... (but [don't use Babel Standalone](https://stackoverflow.com/a/65228599) except for toy snippets you're demonstrating for others, and [use `createRoot`](https://stackoverflow.com/a/71698061) in React 18) – CertainPerformance Apr 07 '22 at 02:03
  • Does this help? Maybe the issue is you're not enumerating your presets. https://stackoverflow.com/questions/40230101/single-react-component-rendering-with-babel-standalone-with-only-index-html-and – Jacob Apr 07 '22 at 02:05
  • Are you seeing any errors? – Macilquham Apr 07 '22 at 02:26
  • @Macilquham No, I don't see any errors in the real version, not stacksnippets – SomeOneOnEarthWhoLives Apr 07 '22 at 02: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. – Macilquham Apr 07 '22 at 02:42

3 Answers3

1

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

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

Satya S
  • 228
  • 1
  • 6
  • 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](/help/how-to-answer). – Community Apr 07 '22 at 05:04
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!

Marcos-MD
  • 86
  • 9
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