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

epi*_*arp 18 javascript reactjs react-jsx

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

{
    this.props.children.map(function(child) {
        return <li>{child}</li>
    })
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!

Ale*_*erg 29

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

在操作prop 时应该使用React.ChildrenAPIchildren.

另见儿童类型道具.