我目前正在寻找通过循环通过已通过此组件的所有者中的道具传递的数组来呈现类中的多个元素。下面是一个例子:尝试通过映射通过数组呈现多个ReactElements时出错
render() {
return (
<div style={this.styles.container}>
{this.props.matchup
? this.renderMatchupWithTeams(this.props.matchup)
: this.renderDefaultMatchup()}
</div>
)
}
然后......
renderMatchupWithTeams(matchup) {
console.log('renderMatchupWithTeams', matchup);
return matchup.map(team => {
console.log(`team: ${team.name}, seed: ${team.seed}`);
return (
<Team name="UCLA"
seed={matchup.seed}/>
)
});
};
日志中包括高亮显示按预期在日志中值回归的事实,但球队组件不。
有关为什么组件未按预期呈现的任何想法?请注意,在这种情况下,forEach的结果与map相同。
**代码已被更新,以反映正确的答案**
你的'renderMatchupWithTeams'函数返回undefined。你应该阅读一下'Array#map'是如何工作的。如果你返回matchup.map,事情就会开始工作。 – Interrobang