【问题标题】:React : How to pass array of ids to method [duplicate]React:如何将ID数组传递给方法[重复]
【发布时间】:2018-04-11 12:36:30
【问题描述】:

我有一个有按钮的表格,按钮有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>

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

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

【问题讨论】:

  • 问题已更新。
  • 那些ID在哪里?

标签: javascript jquery arrays reactjs semantic-ui


【解决方案1】:

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

&lt;Table.Cell&gt; {actionButton(value)} &lt;/Table.Cell&gt;

然后在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>;
      }
    }
}

【讨论】:

    猜你喜欢
    • 2018-11-16
    • 1970-01-01
    • 2017-04-28
    • 2019-10-13
    • 2018-01-31
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    相关资源
    最近更新 更多