当数据准备好时,我试图在渲染函数中调用组件,或者返回 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)
如何修复它?