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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29464577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:34:41  来源:igfitidea点击:

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

javascriptreactjsreact-jsx

提问by epicsharp

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!

任何帮助将不胜感激!

回答by Alexandre Kirszenberg

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

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

You should use the React.ChildrenAPIwhen manipulating the childrenprop.

您应该在操作道具时使用React.ChildrenAPIchildren

See also Type of the Children props.

另请参阅儿童道具的类型



enter image description here

在此处输入图片说明