3

So I just got comfortable with ReactJS within a few and want to actually start creating things with it. Before starting to really get into coding it, I made a basic test to see if React is doing what it is supposed to, and it's not. It's not rendering at all, and I don't see any Syntax errors in the code.
<style> in the index file is just for test of course, I'll use Sass later in external files of course. What am I missing or doing wrong here? Thank you in advance.

var React = require('react');
var ReactDOM = require('react-dom');

var Box = React.createClass({
   render: function () {
       return (
           
           <div>
               <h1>React</h1> 
               <h1>React2</h1>
           </div>
           
       );
   } 
});

ReactDOM.render(<Box />, document.getElementById('app'));
<!DOCTYPE html>
<html lang = "en">
   <head>
      <meta charset = "UTF-8">
      <title>React App</title>
       <script src="https://unpkg.com/react@15/dist/react.min.js"></script>
       <script src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
       <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
       <script src="app.js"></script>
       
       <style>
           body {
               height: 100%;
               width: 100%;
               background-color: rebeccapurple;
           }
           
           #app {
               height: 100px;
               width: 100%;
               background-color: rgba(255,255,255,0.1);
           }
           
           * {
               margin: 0 auto;
               padding: 0;
           }
           
           h1, h2 {
               color: white;
           }
    
       </style>
   </head>
   <body>
      
       <div id="app"></div>
       
   </body>
</html>

7
  • Does it give any errors in your browsers console? if so please add the message. Commented Mar 31, 2017 at 16:54
  • 3
    Don't use require if you already include the file. Commented Mar 31, 2017 at 16:56
  • codepen.io/anon/pen/NpeLjL Seems to work fine. Commented Mar 31, 2017 at 16:58
  • Your JavaScript may be executing before your document is being loaded and thus document.getElementById('box') may not be resolved. Commented Mar 31, 2017 at 17:00
  • I get this error in the Console: app.js:8 Uncaught SyntaxError: Unexpected token < @AndrewLi Ty for letting me know Commented Mar 31, 2017 at 17:17

1 Answer 1

5

Follow these steps it will work:

1. Remove these lines:

var React = require('react');
var ReactDOM = require('react-dom');

2. include the script after the div where you defined the id, like this:

<body>
   <div id="app"></div>
   <script src="app.js"></script>
</body>

3. Include the reference of babel, it required to transpile the JSX code into plain js code, include this reference in header:

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

4. Define the type of the script, like this:

<script src="app.js" type='text/jsx'></script>

Check the working example:

<!DOCTYPE html>
<html lang = "en">
   <head>
      <meta charset = "UTF-8">
      <title>React App</title>
       <script src="https://unpkg.com/react@15/dist/react.min.js"></script>
       <script src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
       <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.0/babel.js"></script>
       <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  </head>
  <body>
      <div id="app"></div>
      <script type='text/jsx'>
         var Box = React.createClass({
             render: function () {
                 return (
                    <div>
                       <h1>React</h1> 
                       <h1>React2</h1>
                    </div>
                 );
             } 
         });

         ReactDOM.render(<Box />, document.getElementById('app'));
      </script>
   </body>
</html>

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

2 Comments

Did those the 1. and 2. already because of the kind suggestions above.. Added your 3rd and 4th step into my code and now it's working perfectly... Thank you very much Mayank :-)
glad, it helped you :)

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.