Apa*_*ear 4 javascript ajax reactjs
我正在我的一个ReactJs组件中对2个不同的api进行2个基本的ajax调用.虽然,在运行调用时(在我确定知道的网址上工作并返回数据),我收到:
Uncaught TypeError: Cannot read property 'groupsData' of null
Run Code Online (Sandbox Code Playgroud)
这是单个组件:
var BrowseWidgetBox = React.createClass({
getGroupsApi: function(){
$.ajax({
url: this.props.groupsApi,
dataType: 'json',
type: 'GET',
success: function(groupsData){
this.setState({groupsData: groupsData});
}.bind(this),
error: function(xhr, status, err){
console.error(this.props.groupsApi ,status, err.toString());
}.bind(this)
});
},
getItemsApi: function() {
$.ajax({
url: this.props.itemsApi,
dataType: 'json',
type: 'GET',
success: function(itemsData){
this.setState({itemsData: itemsData});
}.bind(this),
error: function(xhr, status, err){
console.error(this.props.groupsApi ,status, err.toString());
}.bind(this)
});
},
componentDidMount: function() {
this.getGroupsApi();
this.getItemsApi();
},
render: function() {
return (<div className="BrowseWidgetBox">
<MainMenu groupsData={this.state.groupsData} itemsData={this.state.itemsData} />
<Display />
</div>);
}
});
React.render(
<BrowseWidgetBox groupsApi="/*imagine a working url here*/" itemsApi="/*imagine a working url here*/" />, document.getElementById('widget-container')
);
Run Code Online (Sandbox Code Playgroud)
在reactJS/ajax方面,我有什么明显的缺失吗?
更多实际答案,取决于使用的标准:
ES6课程
export class Component extends React.Component {
constructor(props) {
super(props);
this.state = { groupsData: {}, itemsData: {} };
}
...
}
Run Code Online (Sandbox Code Playgroud)
ES7 +课程
export class Counter extends React.Component {
state = { groupsData: {}, itemsData: {} };
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5364 次 |
| 最近记录: |