我正在尝试使用用户代理将 json 设置为状态,但出现错误:
设置状态的方法:Uncaught Invariant Violation:对象作为 React 子对象无效(发现:对象带有键 {...})。如果您打算渲染一组子项,请改用数组或使用 React 附加组件中的 createFragment(object) 包装对象。
getInitialState: function(){
return {
arrayFromJson: []
}
},
loadAssessmentContacts: function() {
var callback = function(data) {
this.setState({arrayFromJson: data.schools})
}.bind(this);
service.getSchools(callback);
},
componentWillMount: function(){
this.loadAssessmentContacts();
},
onTableUpdate: function(data){
console.log(data);
},
render: function() {
return (
<span>{this.state.arrayFromJson}</span>
);
}
服务
getSchools : function (callback) {
var url = 'file.json';
request
.get(url)
.set('Accept', 'application/json')
.end(function (err, res) {
if (res && res.ok) {
var data = res.body;
callback(data);
} else {
console.warn('Failed to load.');
}
});
}
杰森
{
"schools": [
{
"id": 4281,
"name": "t",
"dfe": "t",
"la": 227,
"telephone": "t",
"address": "t",
"address2": "t",
"address3": "t",
"postCode": "t",
"county": "t",
"ofsted": "t",
"students": 2,
"activeStudents": 2,
"inActiveStudents": 0,
"lastUpdatedInDays": 0,
"deInstalled": false,
"inLa": false,
"status": "unnassigned",
"authCode": "t",
"studentsActivity": 0
},......
]}