7

Noob with React here. I am playing around with React. I have a simple component to render inside my component.js. It is included in my index.html file. I included the scripts for React, ReactDOM, and babel in the head. I just want to see that one div render properly. I am not using Node yet, just a exercise with React and Babel (using babel-standalone). I am running the file with a simple http-server. I am getting an error with the React Chrome extension: Waiting for roots to load...to reload inspector click here.

index.html

<!DOCTYPE html>
<html>
  <head>
    <!-- React -->
    <script src="https://unpkg.com/react@15/dist/react.min.js"></script>
    <!-- React DOM -->
    <script src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
    <!-- babel core-->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.4.4/babel.min.js"></script>
  </head>
  <body>
    <div id="machine-box"></div>
    <script type="text/babel" src="components.js"></script>
  </body>
</html>`

components.js

class MachineBox extends React.Component {
 render(){
   return ( <div>Hello From React </div> );
 }
}

let target =  document.getElementById('machine-box');

ReactDOM.render(
 <MachineBox />, target
)
3
  • Add semicolons where they should be. Add one to the end of the return statement. Commented Oct 25, 2016 at 1:48
  • Do you get anything else in the console? Commented Oct 25, 2016 at 2:07
  • no errors in the console @loganfsmyth Commented Oct 25, 2016 at 2:10

1 Answer 1

8

Your code is fine, you are using a really old version of babel-standalone though.

// this
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.4.4/babel.min.js"></script>

// should be this
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.17.0/babel.min.js"></script>

and

<script type="text/babel" src="components.js"></script>

// should be
<script type="text/babel" src="components.js" data-presets="es2015,react"></script>
Sign up to request clarification or add additional context in comments.

3 Comments

I have setup as above but i can not use import statement, i have to include the .js file including the component. How can i use the import and export statements? Do i have to setup webpack for that to work?
@Joseph That would be the way to do. babel-standalone is really only meant for the most basic testing. In any real production code you should use Webpack.
See stackoverflow.com/a/53975781 for how to import modules using Babel standalone.

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.