Simple "Hello World" in React.js not working

I am making a simple Hello World program in React.js. I expect "Hello World" to be printed in the html body.

index.html

<html>
<head>
<script src="http://fb.me/react-0.12.2.min.js"></script>
<script>
var HelloWorld = React.createClass({
    render: function() {
        return <div>Hello, world!</div>;
    }
});
React.render(new HelloWorld(), document.body);
</script>
</head>
<body> 
</body> 
</html>

Mistake:

Uncaught SyntaxError: Unexpected token <

Can someone tell me where I am making a mistake?

+4
source share
3 answers

What you are missing is what turns JSX into JS. You need to enable JSXTransformer.js. Also note that React.render does not use document.body, it must be a dom element. Here is an example that should work:

<!DOCTYPE html>
<html>
  <head>
    <title>My First React Example</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script>
  </head>
  <body>
    <div id="greeting-div"></div>
    <script type="text/jsx">
      var Greeting = React.createClass({
        render: function() {
          return (
            <p>Hello, Universe</p>
          )
        }
      });
      React.render(
        <Greeting/>,
        document.getElementById('greeting-div')
      );
    </script>
  </body>
</html>
+7
source

Sol answers the question why the simple world application is not running. The best practice in the latest version of response.js as of January 2017 is import

<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.js"></script>

script text/babel.

JSX, ,

class Greeting extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <p>Hello, Universe</p>
    );
  }
}
+6

I just thought that I would share my decision with everyone. I wanted to create a simple project without all npm, node guff, since the OP I had problems, it took me three hours to understand, below is my solution.

HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Boy</title>
    <link rel="stylesheet" type="text/css" href="css/app.css">
    <script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.0/babel.js"></script>
    <script type ="text/babel" src="js/app.js"></script>
</head>
  <body>
    <div id="root">Loading...</div>

  </body>
</html>

The key should include the appropriate script files from React and Babel. I use the external app.js file, if you want to do this, then remember to specify type = "text / babel".

After execution, you can run the hello world example from React. My app.js file:

ReactDOM.render(
    <h1>Hello, world!</h1>,
    document.getElementById('root')
  );

CSS file:

#root {
    color: skyblue;
}

Hope this helps.

0
source

Source: https://habr.com/ru/post/1623880/


All Articles