[英]React won't render within map function
I'm trying to render an array containing some objects using the JS function map(). 我正在尝试使用JS函数map()渲染包含一些对象的数组。 However when I return the text nothing is shown:
但是,当我返回文本时,没有显示任何内容:
console.log(this.props.project.projects); // (2) [{…}, {…}]
this.props.project.projects.map((item, index) => {
console.log(item.projectDescription); //"Testproject"
return (
<div key={index}>
{item.projectDescription}
</div>
)
})
I just don't get it, why there is no text shown, since the console.log(item.projectDescription) shows exactly what I want to display. 我只是没有得到它,为什么没有显示文本,因为console.log(item.projectDescription)显示我想要显示的内容。
Update: It works when I change it to this: 更新:当我将其更改为:
return this.props.project.projects.map((item, index) => (
<div key={index} style={{ color: '#fff' }}>
{item.projektBeschreibung}
</div>
))
I already thought about using the foreach-method but I think it should actually work using the map()-function. 我已经考虑过使用foreach-method但我认为它实际上应该使用map() - 函数。
Here you can see also the render method of my Component. 在这里,您还可以看到我的Component的render方法。
class ProjectRow extends Component {
renderProjects() {
console.log(this.props.project);
if (this.props.project.loading) {
return (
<div style={{color: '#fff'}}>
Loading
</div>
)
} else {
console.log(this.props.project.projects);
this.props.project.projects.map((item, index) => {
console.log(item);
console.log(item.projektBeschreibung);
console.log(index);
return (
<div key={index}>
{item.projektBeschreibung}
</div>
)
})
}
}
render() {
return (
<div>
{this.renderProjects()}
</div>
);
}
}
The renderProjects
function is not returning anything when it hits your else case. 当它碰到你的其他情况时,
renderProjects
函数不会返回任何内容。 Here is an example of use: 这是一个使用示例:
renderProjects() {
console.log(this.props.project);
if (this.props.project.loading) {
return (
<div style={{color: '#fff'}}>
Loading
</div>
)
} else {
console.log(this.props.project.projects);
// added return statement here
return this.props.project.projects.map((item, index) => {
console.log(item);
console.log(item.projektBeschreibung);
console.log(index);
return (
<div key={index}>
{item.projektBeschreibung}
</div>
)
})
}
}
why not use map like below ? 为什么不使用下面的地图?
render(){
return(
<div>
{this.props.project.projects.map((item, index) => (
<div key={index}>
{item.projectDescription}
</div>
))}
</div>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.