admin管理员组文章数量:1023764
I try to convert an array of objects into a list. For now, I just want to convert a "type" property of my object into a list item, but it doesn't work.
Here is my code:
constructor(props){
super(props);
this.travelRawdata = [{type:15}];
}
render(){
return(
<ul>
{this.travelRawdata.forEach(element => <li>{element.type}</li>)}
</ul>
);}
I don't get any output. If I use console.log(element.type)
it works fine, so I think there is something wrong with my HTML tags?
I try to convert an array of objects into a list. For now, I just want to convert a "type" property of my object into a list item, but it doesn't work.
Here is my code:
constructor(props){
super(props);
this.travelRawdata = [{type:15}];
}
render(){
return(
<ul>
{this.travelRawdata.forEach(element => <li>{element.type}</li>)}
</ul>
);}
I don't get any output. If I use console.log(element.type)
it works fine, so I think there is something wrong with my HTML tags?
2 Answers
Reset to default 6forEach()
will always return undefined
. You want to use .map()
instead, to convert your data into an array of react elements:
constructor(props){
super(props);
this.travelRawdata = [{type:15}];
}
render(){
return (
<ul>
{this.travelRawdata.map(element => <li>{element.type}</li>)}
</ul>
);
}
Note: Be sure to read up here on lists and keys. Not including a key for the element, results in rerenders for every single element every time the array is updated.
Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity
Have you tried this?
this.travelRawdata.map((item)=> { return (<li>{item.type}</li> })
I try to convert an array of objects into a list. For now, I just want to convert a "type" property of my object into a list item, but it doesn't work.
Here is my code:
constructor(props){
super(props);
this.travelRawdata = [{type:15}];
}
render(){
return(
<ul>
{this.travelRawdata.forEach(element => <li>{element.type}</li>)}
</ul>
);}
I don't get any output. If I use console.log(element.type)
it works fine, so I think there is something wrong with my HTML tags?
I try to convert an array of objects into a list. For now, I just want to convert a "type" property of my object into a list item, but it doesn't work.
Here is my code:
constructor(props){
super(props);
this.travelRawdata = [{type:15}];
}
render(){
return(
<ul>
{this.travelRawdata.forEach(element => <li>{element.type}</li>)}
</ul>
);}
I don't get any output. If I use console.log(element.type)
it works fine, so I think there is something wrong with my HTML tags?
2 Answers
Reset to default 6forEach()
will always return undefined
. You want to use .map()
instead, to convert your data into an array of react elements:
constructor(props){
super(props);
this.travelRawdata = [{type:15}];
}
render(){
return (
<ul>
{this.travelRawdata.map(element => <li>{element.type}</li>)}
</ul>
);
}
Note: Be sure to read up here on lists and keys. Not including a key for the element, results in rerenders for every single element every time the array is updated.
Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity
Have you tried this?
this.travelRawdata.map((item)=> { return (<li>{item.type}</li> })
本文标签: javascriptRender array of objects to list in JSXStack Overflow
版权声明:本文标题:javascript - Render array of objects to list in JSX - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745565764a2156439.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论