我有一张 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/