admin管理员组文章数量:1406312
Here data in json :
Here my code. After running it i got error in console.log: "Objects are not valid as a React child (found: object with keys {userId, id, title, body}). If you meant to render a collection of children". Why when i console.log(response) inside COmpoonentDidMount i have my array of objects but can't use it inside render() {}. What im doing wrong?
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
users: []
}
}
ponentDidMount() {
var ponent = this;
api.fetchInfo()
.then(function(response) {
ponent.setState( { users: response } );
});
}
render() {
//console.log('here' + this.state.users);
return (
<ul>
<p>{this.state.users}</p>
{
this.state.users.map(
function(elem) {
return <li>{elem.userId}</li>
}
)
}
</ul>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('app')
);
Here data in json : https://jsonplaceholder.typicode./posts
Here my code. After running it i got error in console.log: "Objects are not valid as a React child (found: object with keys {userId, id, title, body}). If you meant to render a collection of children". Why when i console.log(response) inside COmpoonentDidMount i have my array of objects but can't use it inside render() {}. What im doing wrong?
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
users: []
}
}
ponentDidMount() {
var ponent = this;
api.fetchInfo()
.then(function(response) {
ponent.setState( { users: response } );
});
}
render() {
//console.log('here' + this.state.users);
return (
<ul>
<p>{this.state.users}</p>
{
this.state.users.map(
function(elem) {
return <li>{elem.userId}</li>
}
)
}
</ul>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('app')
);
Share
Improve this question
edited May 22, 2017 at 9:32
nosh
6004 silver badges16 bronze badges
asked May 22, 2017 at 9:25
Vasyl GutnykVasyl Gutnyk
5,0393 gold badges37 silver badges38 bronze badges
2 Answers
Reset to default 4This line of jsx:
<p>{this.state.users}</p>
is asking React to render a plain object. And it doesn't know how to do that for you. If you want the json to be printed to the page, you can stringify
it:
<p>{JSON.stringify(this.state.users)}</p>
Otherwise, you might want to map
through and render markup that makes sense for you.
The problem is caused by the line
<p>{this.state.users}</p>
You could remove it OR use:
<p>{JSON.stringify(this.state.users)}</p>
Briefly, the explanation is this:
- Inside any jsx block, you can embed javascript inside curly braces i.e.
{}
- The result of the javascript inside the
{}
block must be: a string, null or another valid jsx object. If it is anything else, you'll see the error you saw.
In your case, you also have a valid block:
{
this.state.users.map(
function(elem) {
return <li>{elem.userId}</li>
}
)
}
The block above is returning valid jsx which is a bunch of <li>
elements. Those <li>
elements themselves have an {elem.userId}
which can be directly interpreted as a string.
本文标签: javascripthow to work with json in reactjsStack Overflow
版权声明:本文标题:javascript - how to work with json in react.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745006394a2637289.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论