ReactJS将父函数传递给使用map创建的子函数

ReactJS将父函数传递给使用map创建的子函数,reactjs,callback,parent-child,parent,Reactjs,Callback,Parent Child,Parent,我需要将父函数传递给子组件,但它似乎不起作用,因为该组件是在映射函数中生成的。以下是相关代码: {this.state.recipes.map(function(a,index){ return (<Recipe name={a.name} ingredients={a.ingredients} index={index} onClick={this.handleDelete}/>); } {this.state.recipes.map(函数(a,索引){ 返回(); }

我需要将父函数传递给子组件,但它似乎不起作用,因为该组件是在映射函数中生成的。以下是相关代码:

{this.state.recipes.map(function(a,index){
   return (<Recipe name={a.name} ingredients={a.ingredients} index={index} onClick={this.handleDelete}/>);
}
{this.state.recipes.map(函数(a,索引){
返回();
}

解决这个问题的最佳方法是什么?

不管它是否是映射函数中的组件。我认为您只是忘记了绑定函数

您的代码应该是这样的

{this.state.recipes.map(function(a,index){
   return (<Recipe name={a.name} ingredients={a.ingredients} index={index} onClick={this.handleDelete.bind(this}/>);
}
{this.state.recipes.map(函数(a,索引){
返回();
}
您也可以使用箭头功能,如下所示:

{this.state.recipes.map((a,index) => <Recipe name={a.name} ingredients={a.ingredients} index={index} onClick={this.handleDelete.bind(this}/>)}
{this.state.recipes.map((a,index)=>)}

这里是。

它是否是映射函数中的组件并不重要。我认为您只是忘记了绑定函数

您的代码应该是这样的

{this.state.recipes.map(function(a,index){
   return (<Recipe name={a.name} ingredients={a.ingredients} index={index} onClick={this.handleDelete.bind(this}/>);
}
{this.state.recipes.map(函数(a,索引){
返回();
}
您也可以使用箭头功能,如下所示:

{this.state.recipes.map((a,index) => <Recipe name={a.name} ingredients={a.ingredients} index={index} onClick={this.handleDelete.bind(this}/>)}
{this.state.recipes.map((a,index)=>)}
这是