javascript - react : How to pass array of ids to method

标签 javascript jquery arrays reactjs semantic-ui

我有一张 table ,上面有一个按钮,按钮有 onClick()函数,我将一个方法传递给 onclick ,但是 ids 在一个数组中。

我可以console.log喜欢 props.requests[0].objectId .

const actionButton = (published) => {
    if (props.userType === 'crick') {
      if (props.data.verified) {
        return published === true ? <Label>Published</Label> : <Button basic onClick={() => scoreAction(// Have to pass ids here)} >Publish</Button>;
      }
    }
}

下面的表格来自上面的代码。

   <Table.Body>
      {props.loanRequests.map((value, i) =>
    (<Table.Row key={i}>
      <Table.Cell> {value.cric.name}  </Table.Cell>
      <Table.Cell> {value.runs}  </Table.Cell>
      <Table.Cell> {actionButton(value.published)} </Table.Cell>
    </Table.Row>))}
   </Table.Body>

如何将 ids 数组传递给按钮的 onclick 函数方法。

注意:语义 UI 正在使用中。

最佳答案

为什么不将整个对象传递给actionButton?

<Table.Cell> {actionButton(value)} </Table.Cell>

然后里面actionButton您可以引用属性。

const actionButton = (request) => {
    const {id, published} = request;
    if (props.userType === 'crick') {
      if (props.data.verified) {
        return published === true ? <Label>Published</Label> : <Button basic onClick={() => scoreAction(id)} >Publish</Button>;
      }
    }
}

关于javascript - react : How to pass array of ids to method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47016820/

相关文章:

javascript - Jquery 响应式导航栏在单击菜单项或单击菜单按钮时折叠

javascript - js 中的否定lookbefore

javascript - 将每个单词推送到一个数组

javascript - jQuery - 如何在 HTML 中移动结束标签

javascript - IE10 在文档打开后但在文档写入之前执行功能问题?

javascript - Chrome 扩展 : the js script fires too early despite run_at: document_idle

javascript - 在追加之前编辑对象

javascript - 如何获取第一个数组中第一个对象的第一个值

java - 从方法返回一个值并将其存储到数组中以供另一个方法使用

javascript - 为什么需要 .length 来查找 for 循环中数组元素的等价项?