Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Andrew Chen
Andrew Chen
9,446 Points

React code is unable to run on local server but runs OK on local file

Hello,

I've set up my local server as per the MDN tutorial by using the "python3 -m http.server" command. The local server connects fine when loaded on my browser but it does not render the react components. All other HTML, CSS components load fine.

However, the components render fine when I load the exact same files through local file. One difference I noticed was that in the local server, in the React Developer Tools, it was stuck at a "Waiting for roots to load..." message.

For reference, these are the codes:

Index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Scoreboard</title>
  <link rel="stylesheet" href="./app.css" />
</head>

<body>
  <div id="root"></div>
  </div>
  <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <script src="./app.js"></script>
</body>
</html>

app.js

  const desc = React.createElement(
    'p',
    null,
    'I just learned how to create a React node and render it into the DOM.'
  );

  const header = React.createElement(
    'header',
    null,
    title,
    desc
  );

  ReactDOM.render(
    header,
    document.getElementById('root')
  );

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points

I see this error on the console:

Uncaught ReferenceError: title is not defined
    at app.js:10

Not sure why it worked the other way. Maybe it was serving up an old version that was cached.

This stuff gets easier when you get farther along in React. There is a hot-reloading server built into the tools.

Also, consider adding tools or plugins like eslint that will give you helpful squiggly lines right in your text editor if you have variables that you're trying to use but never declare.