我已经在其他几个组件中编写了这段代码,但似乎无法理解为什么它不起作用。
{
this.props.children.map(function(child) {
return <li>{child}</li>
})
}
如有任何帮助,我们将不胜感激!
最佳答案
this.props.children
是一个不透明的数据结构。它可以是数组或单个元素。在您的情况下,this.props.children
可能是单个元素,这就是 .map()
方法未定义的原因。
你应该使用 React.Children
API在操作 children
Prop 时。
另见 Type of the Children props .
关于javascript - 为什么 this.props.children.map 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29464577/