我有一个表格,其中有一个按钮和按钮具有onClick()
功能,我将方法传递给onclick
,但是这些ID在数组中。
我可以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函数方法。
注意:正在使用语义用户界面。
答案 0 :(得分:2)
为什么不将整个对象传递给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>;
}
}
}