Zayn
Zayn

Reputation: 741

TypeError: Cannot read property 'history' of undefined in Jquery - React

I have been using datatables.net for my react project. I created two action buttons on every row (edit, remove). Now whenever this listener run then "this" does not work. If I made jquery function to arrow function then "this.props..." works but then data is undefined.

$('#data-table-zero').on('click', '.edit-button', function() {
        const data = $('#data-table-zero').DataTable().row($(this).parents('tr')).data();
        this.props.history.push({
            pathname: `/add-product`,
            state: {
                selectedRecord: { ...data }
            }
        });
    });

Upvotes: 0

Views: 74

Answers (1)

Niraj Kaushal
Niraj Kaushal

Reputation: 1502

Please read this

I hope this will help you,

let that = this
$('#data-table-zero').on('click', '.edit-button', function() {
        const data = $('#data-table-zero').DataTable().row($(this).parents('tr')).data();
        that.props.history.push({
            pathname: `/add-product`,
            state: {
                selectedRecord: { ...data }
            }
        });
    });

Upvotes: 2

Related Questions