为什么 this.props.children.map 不起作用?

Why doesn't this.props.children.map work?

我已经在其他几个组件中编写了这段代码,但似乎无法理解为什么它不起作用。

{
    this.props.children.map(function(child) {
        return <li>{child}</li>
    })
}

如有任何帮助,我们将不胜感激!

this.props.children 是一个不透明的数据结构。它可以是数组或单个元素。在您的情况下,this.props.children 可能是单个元素,这就是 .map() 方法未定义的原因。

操作 children 道具时应使用 React.Children API

另见 Type of the Children props