0

I wrote a react class but Chrome is throwing a syntax error in the render function.

I'm attaching the code here:

var Chatrooms = React.createClass({

    getInitialState: function() {
        return {
            loaded: false,
            pages: undefined,
            id: "-1",
        };
    },

    loadPages: function() {
        url = "/chat/api/chatroom/" + this.props.course + "/get/?format=json";
        request = ajax_json_request(url, "GET", {});
        request.done(function(response) {
            response = jQuery.parseJSON(response);
            oldState = this.state;
            oldState['chatrooms']=response;
            oldState['loaded']=true;
            this.setState(oldState);
        }.bind(this));
    },

    componentDidMount: function() {
        if(!this.state.loaded) {
            this.loadPages();
        }
    },

    render: function(){
        console.log(this.state.chatrooms);
        return (
                <div></div>
            );
    }

});

Browser is showing the error in line <div></div> as Uncaught SyntaxError: Unexpected token <. I checked the syntax online and it looks good to me.

1
  • Are you transpiling the code from JSX to regular JavaScript? Commented Mar 3, 2015 at 12:50

2 Answers 2

1

Try adding the JSX pragma on the top of the JSX file:

/** @jsx React.DOM */

This is tells the browser to use the JSX directive.

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

1 Comment

You shouldn't need that any more as part of 0.12.
1

You must have used <script type="text/javascript"> instead of <script type="text/jsx">

1 Comment

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.