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

标签 javascript reactjs

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

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

如有任何帮助,我们将不胜感激!

最佳答案

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

你应该使用 React.Children API在操作 children Prop 时。

另见 Type of the Children props .


enter image description here

关于javascript - 为什么 this.props.children.map 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29464577/

相关文章:

javascript - 莫里斯栏上的点击事件

javascript - require.main 和 module 有什么区别?

reactjs - 在 React 中使用列表和键时,键可以包含空格吗?

javascript - ionic 导航后退按钮未显示

javascript - 带 3d 安全卡和 next_action null 的 Stripe 订阅

带有三重 => 的 Javascript 函数语法

javascript - React 网站有时不会在移动设备上全屏显示

reactjs - 使用 mobX 和 React Context 从类中访问商店

forms - React formik - 在http请求后用数据填充表单输入

javascript - IE 8 文档模式 :IE 8 standards 的正则表达式中应为 ']'