数据准备好后,react-rails gem调用组件

时间:2015-01-22 00:50:39

标签: javascript ruby-on-rails reactjs react-rails

我正在尝试在数据准备就绪时调用渲染函数中的组件,或者返回null。我不知道为什么,但react-rails gem在这个字符串中返回Parse Error。源代码如下:

var PlayersData = React.createClass({

  loadDataFromServer: function() {
    console.log("loadDataFromServer");
    $.ajax({
      url: this.props.url,
      dataType: 'json',
      success: function(data) {
        this.setState({data: data});
      }.bind(this),
      error: function(xhr, status, err) {
        console.error(this.props.url, status, err.toString());
      }.bind(this)
    });
  },

  getInitialState: function() {
    console.log("getInitialState");
    return {data: {}};
  },

  componentWillMount: function() {
    console.log("componentWillMount");
    this.loadDataFromServer();
  },

  render: function() {
    console.log("render");
    return (
      {this.state.data ? : <PlayersRepresentation data={this.state.data} /> : null}
    );
  }
});

错误跟踪日志:

Showing /Users/user/Documents/Projects/rails_react/app/views/layouts/application.html.erb where line #12 raised:

Error: Parse Error: Line 30: Unexpected token .
  (in /Users/user/Documents/Projects/rails_react/app/assets/javascripts/components/match_center/players_data.js.jsx)

如何解决?

1 个答案:

答案 0 :(得分:0)

此冒号不属于:

 //                 V
 {this.state.data ? : <PlayersRepresentation data={this.state.data} /> : null}

如果删除它,它是否正确解析?