seriouslyForWhat
seriouslyForWhat

Reputation: 34

Importing ReactDOM doesn't render anything

I'm pretty new to React and I've been trying to set up a REACT app. However, I always get a blank page. Can anyone help?

HTML Code (index.html)

<html>
  <head>
    <meta charset="UTF-8" />
    <title>Home</title>
  </head>
  <body>

    <div id="root"></div>
    <script src="index.js" type="text/babel"></script>

  </body>
</html>

Javascript (index.js)

import React from "react"
import ReactDOM from "react-dom"

const page = (
  <div>
    <h1>My page</h1>
  </div>
)

ReactDOM.render(page, document.getElementById("root")); 

And yes, I am using a live server to run this code.

Upvotes: 1

Views: 203

Answers (1)

Quentin
Quentin

Reputation: 943561

Browsers don't understand the text/babel MIME type.

It is there for Babel to search the DOM for scripts that it should process to convert from whatever they are (JS + JSX in this case) to JS.

You haven't included Babel in your page though.

You have a further problem in that inside your script you have import statements which depend on Node.js module resolution (and you're using a browser, not Node).


You should start at the beginning of the React guide.

You currently have an odd mix of about 20% of the quick guide to adding React to a website and 5% of using a Node.js based toolchain to transpile your code.


I recommend starting with create-react-app as it gives you a robust, performant foundation to get started with.

Upvotes: 1

Related Questions