javascript - react : passing parameters to a function in map

标签 javascript reactjs parameters map-function

我正在构建一个组件,使用 MaterialDesign 的表格在 React 中显示用户数组。

Users Table 每行由一个映射函数生成:

let actions = this.props.rowButtons;
...
{this.props.tableData.map( (val, index) => (
                       <TableRow key={index}>
                         <TableRowColumn >{index}</TableRowColumn>
                         <TableRowColumn >{val.firstName}</TableRowColumn>
                         <TableRowColumn >{val.lastName}</TableRowColumn>
                         <TableRowColumn >{val.email}</TableRowColumn>
                         <TableRowColumn >
                            <ActionButtons actions={actions}/>
                         </TableRowColumn>
                       </TableRow>
))}

<ActionButtons>组件创建一个或多个按钮,并作为参数采用一组对象,如 [{type: string, onClick: function}, {type: string, onClick: function}, ... ] (数组中每个对象一个按钮); 父组件正在 this.props.rowButtons 中发送这样的数组。

我想将 val 作为参数传递给每个 onClick 函数以获得类似的内容

actions = [{type: "personButton", onClick: onClick(val)}, {type: '', onclick: someOtherfunction(val)}]; 

最佳答案

您必须稍微修改一下操作。尝试按照以下方式进行操作。

modifyActions = (actions, val) => {
  return actions.map((a) => {
      return Object.assign({}, a, {onClick: () => a.onClick(val)})
  })
}

<ActionButtons actions={this.modifyActions(actions, val)})}/>

关于javascript - react : passing parameters to a function in map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47728814/

相关文章:

javascript - 使用 jquery 切换显示子项

javascript - AngularJS 中的 Hacker Typer Effect(在随机按键时显示特定文本)

javascript - 在 ng-repeat 中将项目添加到数组后保持滚动位置

reactjs - 删除生产版本中的 html 属性

c++ - C/C++ 中数组的参数传递和作用域

javascript - jQuery中如何动态传递 'options:contains'中的变量获取值

reactjs - 无法创建新的reactJs应用程序

ReactJS : In react-rooter-dom, 是否已弃用 useHistory?

ruby-on-rails - 如何找到数组中最后一项的索引?

typescript - typescript 是否允许为 lambda 参数定义类型?