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
Why doesn't this.props.children.map work?
提问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.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.
this.props.children
是一种不透明的数据结构。它可以是数组或单个元素。在您的情况下,this.props.children
可能是单个元素,这就是该.map()
方法未定义的原因。
You should use the React.Children
APIwhen manipulating the children
prop.
您应该在操作道具时使用React.Children
APIchildren
。
See also Type of the Children props.
另请参阅儿童道具的类型。