ant-design: Table onRow Event multiple triggered

Version

3.1.0

Environment

macos 10.12.6, chrome 63

Reproduction link

codepen

Steps to reproduce

My component which using Table component in 2.x versions, when I migrate to 3.x versions, console print out such errorwebpack-internal:///15:49 Warning: onRowClick is deprecated, please use onRow instead., when I replace onRowClick with onRow event, my Component BREAK DOWN, as I use setState with onRowClick[onRow]

What is expected?

event should be triggered when I click table item

What is actually happening?

it triggered when each line rendered


when I look into source code in Table component, https://github.com/ant-design/ant-design/blob/1eff817b9ada2169f0ddeb5e2e9fd2df88270134/components/table/Table.tsx#L218, why you execute props onRow event, cuz used in onRowClick is bind event, or maybe you have changed the usage purpose of this function

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 24 (4 by maintainers)

Most upvoted comments

Perfect!!!

It works with:

onRow={(record) => ({
    onClick: () => { myCallBackThatReceivesTheID(record.key); }
})}

Thanks a lot @yesmeck !!!

@TimKraemer ,as @dengfuping said, the onRow event is to set props for each row, it means the work of onRow is changed, and you need to pass rowSelection[Object] props to deal with select events. You can checkout my modified codepen,OR check the document this part: https://ant.design/components/table/#rowSelection. Hope this helps. Also I think they should change the warning description as this props function totally changed, not instead.

<Menu.Item onClick={(e)=>{ 
             e.domEvent.stopPropagation();
              handleUpdate(id)}}>
              Edit
 </Menu.Item>

@mikaelascorn I’m doing it like this…

const onRow = (record: any, rowIndex: any) => {
	return {
		onClick: () => { setSelectedRow(rowIndex) }
	}
}

const rowClassName = (record: any, index: number): string => {
	return index === selectedRow ? `${gStyles.active}` : ``;
}

<Table rowClassName={rowClassName} onRow={onRow} ......></Table>

@springxiege if you work with method @aaronplanell describe, you MUST stopPropagation in your click event, if you still confused with what I describe, check codepen in reproduction link above. Hope it helps!

When I am clicking on Checkbox of any row of the table then onRow functionality of the table is being triggered at the same time because of event bubbling. I am not able to stop this because they are not giving event in rowSelection .

If you still getting this issue : Warning: onRowClick is deprecated, please use onRow instead. you can fixed this issue by trying this:

onRow={(record) => ({ onClick: () => { this.someClick(record.key) } })}

The following example shows how to use a v2 style onRowClick function with v3 style onRow

 <Table
        onRow={(record, index) => ({
          onClick: (event) => { onRowClick(record, index, event) } 
        })}
       ....

@nischalhubbler @stefatkins What’s your antd version, I’ve just fix my fixed codepen https://codepen.io/dogbutcat/pen/wprmrR which using antd@3.1.0. Hope this example will help.

Is there a way to highlight the row once that row is selected?

@aaronplanell You can pass arbitrary props to <tr /> through onRow prop, so we don’t encourage using onRowClick and other onRowWhateverEvent any more. See more details in our 3.0 release log. https://ant.design/changelog

@cllgeek en…Best to provide your simple reproduce example or component, whether had checked the codepen I fixed above: https://codepen.io/dogbutcat/pen/wprmrR. As it works fine for me now.