Home > Software design >  ReactDOM render not working using CDN links
ReactDOM render not working using CDN links

Time:07-08

Below is my html and index.js code. I can't seem to find the mistake as right now nothing is rendering on my html page.

<!DOCTYPE html>
<html>
<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>Document</title>
    <script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
</head>
<body>
    <div id="root"></div>
    <script src="index.js" type="text/babel"></script>
</body>
</html>

index.js

ReactDOM.render(<h1>Hello</h1>, document.getElementById("root"))

CodePudding user response:

Change your index.js to this:

import { createRoot } from "https://cdn.skypack.dev/[email protected]"
createRoot(document.getElementById('root')).render(<h1>Hello</h1>)

CodePudding user response:

You need the babel standalone link as well for this to work. Add this just above your index.js import in your html file:

<script
  src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"
  integrity="sha512-kp7YHLxuJDJcOzStgd6vtpxr4ZU9kjn77e6dBsivSz pUuAuMlE2UTdKB7jjsWT84qbS8kdCWHPETnP/ctrFsA=="
  crossorigin="anonymous"
  referrerpolicy="no-referrer"
></script>
  • Related