Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't see attached event function but noop

The problem is that although I have attached events (e.g. handleClear) in the class below, however, when I check the onClick event in the button dom, it shows an empty method noop() {} instead. As a result, it can't clear as expected.

The strange thing is that sometimes if I set breakpoints, the handler method would be there. I suspect the event attachment is related to the timing. But I can't identify when and where it misses to attach the event function. Could anyone provide some tips on resolving this? Or provide some insights?

class ClearHistory extends PureComponent {

    handleCancel = () => {
        console.log('handleCancel');
    }

    handleClear = (event) => {
        console.log('handleClear');
    }

    render() {

        return (
            <div>
                <Button key='cancel' type='button' onClick={this.handleCancel} >
                    Cancel
                </Button>
                <Button key='remove' type='button' onClick={this.handleClear} >
                    Clear
                </Button>
            </div>
        );
    }
}
like image 376
Haoyu Chen Avatar asked Oct 19 '25 14:10

Haoyu Chen


1 Answers

I created sandbox to test your component, and I renamed to a html tag because the component without any ui-library.

enter image description here

https://codesandbox.io/s/clearhistory-fq69b?file=/src/ClearHistory.js

enter image description here

It works but:

enter image description here

This is fine because the react itself takes care of the event binding.

enter image description here

https://the-guild.dev/blog/react-dom-event-handling-system

like image 98
Daniil Loban Avatar answered Oct 21 '25 03:10

Daniil Loban