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

我怕爱的太早我们不能终老 提交于 2019-12-05 10:20:41

问题


I have written this code in several other components but can't seem to understand why this isn't working.

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

Any help would be greatly appreciated!


回答1:


this.props.children is an opaque data structure. It can be either an array or a single element. In your case, this.props.children is probably a single element, which is why the .map() method is undefined.

You should use the React.Children API when manipulating the children prop.

See also Type of the Children props.




来源:https://stackoverflow.com/questions/29464577/why-doesnt-this-props-children-map-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!