Hi I am trying to set up my react app such that when you click on a button in a row item in my react-table the data in that row is passed onto another component. At the moment I am simply trying to console.log the correct data but am unsure of how to pass react-table row data based on click. How can I do this? Thanks
My Dummy data stored in state along with the button (Show Detailed View) which I want to trigger the data passing onclick:
    columns: [
      {
        Header: "First Name",
        accessor: "fname"
      },
      {
        Header: "Last Name",
        accessor: "lname"
      },
      {
        Header: "Employee Group",
        accessor: "egroup"
      },
      {
        Header: "Date of Birth",
        accessor: "dob"
      },
      {
        Header: "",
        id: "id",
        Cell: ({ row }) => (
          <button onClick={e => this.handleShow()}>
            Detailed View
          </button>
        )
      },
    ],
    posts: [
      {
        fname: "gerald",
        lname: "nakhle",
        egroup: "faisbuk",
        dob: "8/10/1995"
      }
    ]
My Call to render the table:
<ReactTable columns={this.state.columns} data={this.state.posts}></ReactTable>
My onclick handler function but im not sure how I can access the table row data I'm after
handleShow(e) {
    console.log(e);
  }
                You will need to add an onClick handler for the row
const onRowClick = (state, rowInfo, column, instance) => {
    return {
        onClick: e => {
            console.log('A Td Element was clicked!')
            console.log('it produced this event:', e)
            console.log('It was in this column:', column)
            console.log('It was in this row:', rowInfo)
            console.log('It was in this table instance:', instance)
        }
    }
}
<ReactTable columns={this.state.columns} data={this.state.posts} getTrProps={onRowClick}></ReactTable>
check this post for more info react-table component onClick event for a column
In your Table definition :
export function TableCustom({handleShow}) {
    const columns = React.useMemo(() => [{
        Header: 'Action',
        accessor: 'action',
        Cell: props => <button className="btn1" onClick={() => handleShow(props)}>Details</button>,
    },]
    return <ReactTable>;
});
And in your parent component : view the data of the clicked row :
const handleShow = (cell) => {
    console.log(cell?.row?.original);
}
                        For React-table v7:
Pass a callback prop onRowClicked to the table component,
In your table component call the callback:
...row.getRowProps({
         onClick: e => props.onRowClicked && props.onRowClicked(row, e),
})
In react-table v7, all the spread operator on HTML element that starts with
get...Propsare props getter, for example:row.getRowProps(), cell.getCellProps(), column.getHeaderProps(), getTableBodyProps(), getTableProps() etc.. You can pass any more attributes to extend it. for example:
    ...cell.getCellProps({ 
        style: {color: 'red'},  
        onClick: ()=> {}   
    }) 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With