Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am trying to use rowEvents to trigger an action in react-bootstrap-table-2 but 'this' in the onClick is undefined. Any ideas?

Here is the codeblock:

const expandRow = {
    renderer: row => (
        <div>
            <BootstrapTable
                keyField='id'
                data={ row.data }
                columns={ columns1 }
            />
        </div>
    )
};

export default class MyPage extends Component {
    constructor(props, context) {
        super(props, context);
    }

    componentDidMount() {
        this.props.actions.fetchData().  // fetches in this.props.data
    }

    rowEvents = {
        onClick: (e, row, rowIndex) => {
            this.props.actions.fetchHistory(row.Id).   /* this is undefined */
        }
    }

    render() {

        return (
            <BootstrapTable
                keyField='Id'
                data={ this.props.data }
                columns={ columns }
                striped
                rowEvents={ this.rowEvents }
                expandRow={ expandRow }
            />
        )
    }
}

What I am trying to do is for each row clicked, I want to fetch data by triggering an action. I might be wrong about understanding the context of 'this' here. Any ideas?

like image 695
PraJaL Avatar asked Jul 24 '20 20:07

PraJaL


1 Answers

rowEvents is not a function but a constant, try to define it with const

const rowEvents = {
        onClick: (e, row, rowIndex) => {
            this.props.actions.fetchHistory(row.Id).   /* this is undefined */
        }
    }
like image 86
YASH DAVE Avatar answered Sep 28 '22 01:09

YASH DAVE