1

I try to include an external react js file into a html file, but there is nothing shown in index.html. Could anyone help me with it?

The code in react file (index.html)looks like this:

<!DOCTYPE html>
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.6/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.6/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.js"></script>
  </head>
  <body>
    <div id="app"></div>
    <script src="index.js" type="text/babel"></script>
  </body>
</html>

The code in js file (index.js)looks like this:

var App = React.createClass({
  render: function() {
    return <div>Something something Dark Side</div>;
  }
});

ReactDOM.render(
  <App />,
  document.getElementById('app')
);
3

1 Answer 1

3

I have a feeling you're opening this html file locally. If this is the case the URL in the browser will look something like file:///Users/YourUsername/PathToYourHTMLFile/index.html

If you open up the console from your browser devtools you'll most likely see a CORS policy error. More on CORS here. TLDR when you're using the browser will send a AJAX request to the url provided in src="" attribute, for security reasons the browser will, by default, only allow you to send requests to resources that share the same server but because you're not running a server for the HTML and Javascript file CORS blocks the request.


Update: The workaround for the CORS problem provided by https://reactjs.org/docs/add-react-to-a-website.html is to add scripts within the body with the "crossorigin" attribute.

After doing so you will find the React component still will not render correctly. If you look in the console you will have an error "Uncaught SyntaxError: Unexpected token <". That's because you're trying to use JSX to render elements. Browsers only understand html and javascript; JSX is a "syntax extension to JavaScript" and must be compiled before a browser can understand it.

TLDR: The syntax is causing your components to not load. Try this:

index.js

var App = React.createElement('div', false, 'something something darkside');

ReactDOM.render(
  App,
  document.getElementById('app')
);

Above we're replacing JSX, with top level functions (React.createElement) provided by the React api. When JSX is compiled it will output the same thing.

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Document</title>
</head>
<body>
  <div id="app"></div>
  <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>

  <!-- Load our React component. -->
  <script src="./index.js"></script>
</body>
</html>

If you're just starting out with React I highly recommend the tutorial on reactjs.org

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

2 Comments

Yes! I just open the html file locally.so how can I fix it? I also tried the way in this link reactjs.org/docs/add-react-to-a-website.html, it didn't prompt the error, but it seems like the js file doesn't load into browser successfully.
Hey Chelsea sorry for the late response, I'll update the answer with a workaround & explain why the code isn't working as expected

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.