Bevilacqua Bevilacqua - 3 months ago 10
React JSX Question

React.js on Rails error

I am following this tutorial on how to integrate react.js into rails and when I pre compile my assets I get an error with this code stating that

(data) => this.setState({statuses: data});
causes
SyntaxError: unknown: Unexpected token (14:44)
I tried many things to get it to work and when I delete the semi-colon at the end it works except that the interval no longer gets called the console will say alert twice when the page is refreshed and then never again.

var StatusesContainer = React.createClass ({
componentWillMount() {
this.fetchStatuses();
setInterval(this.fetchStatuses(), 1000);
},

fetchStatuses() {
console.log("alert");
$.getJSON(
this.props.statusesPath,
{
space_id: this.props.space_id
},
(data) => this.setState({statuses: data});
);
},

getInitialState() {
return ({ statuses: [] });
},

render() {
return (<RecentStatuses statuses={ this.state.statuses }/>);
}
});

Answer
$.getJSON(
    this.props.statusesPath,
    {
        space_id: this.props.space_id
    },
    (data) => this.setState({statuses: data});
);

Here is the problem: (data) => this.setState({statuses: data});, you should use , not ;

Comments