0

My code is throwing syntax error at line <div>Hello World!</div>

This is my code

<!DOCTYPE html>
<html>
<head>
    <script src="libs/react.min.js"></script>
    <script src="libs/react-dom.min.js"></script>
</head>
<body>
    <div id="main">

    </div>

<script>
    var MessageComponent = React.createClass({
        render: function() {
            return (

                <div>Hello World!</div>
            );
        }
    });

    ReactDOM.render(
        <MessageComponent message="Lorem" />,
        document.body
    );
</script>    
</body>
</html>

What could be the issue?

phantomCoder
  • 1,499
  • 3
  • 18
  • 32

2 Answers2

3

You are using the JSX syntactic sugar to build your UI component and browsers don't understand that.

You will have to transpile it to JS first before it can work.

Sushanth --
  • 55,259
  • 9
  • 66
  • 105
0

I added Babel in the page

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

and added type="text/babel" to the script tag. That fixed the issue

phantomCoder
  • 1,499
  • 3
  • 18
  • 32