3

Just started my ReactJS and stuck at my very first try. I have a very basic code that throws "Uncaught SyntaxError: Unexpected token <".

index.html

<!doctype html>
<head>
    <title>My first ReactJs</title>
</head>
<body>
    <div id="container"></div>
    <script src="react.js"></script>
    <script src="script.jsx"></script>
</body>
</html>

script.jsx

var MessageButton = React.createClass({
    render: function(){
        return  (
            <button>Hello World</button>
        );
    }
});

React.render(<MessageButton/>, document.getElementById("container"));

Assuming that it could be missing JSX transformer library, I searched for it but couldn't find download anywhere. I work offline most of the time, so I do not wish to use plunkr or jsbin. Could do with some help.

4
  • do you have a compiler? Commented Nov 11, 2015 at 9:07
  • what line is popping your error ? I don't think you need to use .jsx extension Commented Nov 11, 2015 at 10:18
  • <button>Hello World</button> is he line popping the error. I tried changing it to other valid html but none helped. Commented Nov 11, 2015 at 10:25
  • have you change the extension with .js instead of jsx ? Commented Nov 11, 2015 at 10:30

1 Answer 1

3

First: Specify the type attribute of your JSX scripts so the browser doesn't try to execute them as JavaScript.

<script type="text/jsx" src="script.jsx"></script>

Second:

Either:

Load the JSXTransformer script (which requires an older version of React)

or

Compile the JSX using Babel:

Example taken from the docs:

babel --presets react src --watch --out-dir build
Sign up to request clarification or add additional context in comments.

Comments

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.