问题
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