Promise Error: Objects are not valid as a React child
Promise Error: Objects are not valid as a React child
我正在尝试将 json 设置为使用用户代理的状态,但出现错误:
Uncaught Invariant Violation: Objects are not valid as a React child (found: object with keys {...}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons.
设置状态的方法:
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.');
}
});
}
Json
{
"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
},......
]}
你不能这样做:{this.state.arrayFromJson}
因为你的错误表明你正在尝试做的事情是无效的。您正在尝试将整个数组呈现为 React 子项。这是无效的。您应该遍历数组并呈现每个元素。我使用 .map
来做到这一点。
我正在粘贴 link 从中您可以了解如何使用 React 从数组中呈现元素。
http://jasonjl.me/blog/2015/04/18/rendering-list-of-elements-in-react-with-jsx/
希望对您有所帮助!
你不能只是 return 一个对象数组,因为没有任何东西告诉 React 如何渲染它。您需要 return 一组组件或元素,例如:
render: function() {
return (
<span>
// This will go through all the elements in arrayFromJson and
// render each one as a <SomeComponent /> with data from the object
{this.state.arrayFromJson.map(function(object) {
return (
<SomeComponent key={object.id} data={object} />
);
})}
</span>
);
}
我正在尝试将 json 设置为使用用户代理的状态,但出现错误:
设置状态的方法:Uncaught Invariant Violation: Objects are not valid as a React child (found: object with keys {...}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons.
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.');
}
});
}
Json
{
"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
},......
]}
你不能这样做:{this.state.arrayFromJson}
因为你的错误表明你正在尝试做的事情是无效的。您正在尝试将整个数组呈现为 React 子项。这是无效的。您应该遍历数组并呈现每个元素。我使用 .map
来做到这一点。
我正在粘贴 link 从中您可以了解如何使用 React 从数组中呈现元素。
http://jasonjl.me/blog/2015/04/18/rendering-list-of-elements-in-react-with-jsx/
希望对您有所帮助!
你不能只是 return 一个对象数组,因为没有任何东西告诉 React 如何渲染它。您需要 return 一组组件或元素,例如:
render: function() {
return (
<span>
// This will go through all the elements in arrayFromJson and
// render each one as a <SomeComponent /> with data from the object
{this.state.arrayFromJson.map(function(object) {
return (
<SomeComponent key={object.id} data={object} />
);
})}
</span>
);
}