我已经在其他几个组件中编写了这段代码,但似乎无法理解为什么它不起作用。
{
this.props.children.map(function(child) {
return <li>{child}</li>
})
}
非常感谢您的帮助!
我已经在其他几个组件中编写了这段代码,但似乎无法理解为什么它不起作用。
{
this.props.children.map(function(child) {
return <li>{child}</li>
})
}
非常感谢您的帮助!
this.props.children
是一个不透明的数据结构,它可以是数组或单个元素。在你的情况下,this.props.children
可能是单个元素,这就是为什么 .map()
方法未定义的原因。
当操作 children
属性时,应使用 React.Children
API。
另请参阅Children props 的类型。
[...this.props.children].map(function(elem) {
console.log(elem);
});
children
不是数组时,这将导致运行时错误;而当children
已经是一个数组时,将其展开到一个新创建的数组中是一种无用的活动,你可以直接在children
上调用.map()
。 - Martin
Uncaught TypeError: undefined is not a function
。 - epicsharpthis.props.children
不是一个数组(或者不是另一个具有map
属性的对象)。 - zerkmsReact.Children.map
?https://facebook.github.io/react/docs/top-level-api.html - WiredPrairie