只是一个快速查询。
我有一个数据点数组,并使用它来创建组件的实例。
创建子组件数组的父组件也有一些我希望传递给其子组件的功能。有什么办法可以做到这一点吗?检查代码并告诉我,我收到错误。
class App extends React.Component {
constructor(props) {
super(props)
this.handleNext = this.handleNext.bind(this)
}
handleNext() {
// some function that uses setState and will cause component to re render
}
render() {
let children = someArray.map(function(elem, index) {
return (
<ChildComponent name = {elem.name} handDownFunc = {this.handleNext}/>
)
})
return (
{children} // I want them to each be able to access and use handleNext
)
}
}
最佳答案
您收到此错误,因为使用 function
关键字而不是箭头函数。
更多信息请关注link .
所以,它将是 someArray.map(() => { return() })
关于javascript - 从 array.map() 创建组件实例并传递函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58308120/