问题
for example:
I add event handler in react:
<div onClick={someHandler}/>
Then I dispatch event:
let clickEvt = new MouseEvent('click', {
'bubbles': false,
'cancelable': true
});
elm.dispatchEvent(clickEvt);
But nothing happens. I hear you can use elm.click()
to trigger the react event. I'm wondering if that is the proper way to do it in react? Also what is the difference between click()
and dispatchEvent()
? Because I kinda want to stick to dispatchEvent()
.
回答1:
All
I figured it out. bubbles
must be set to true
for react to receive the event, which is why click()
method works, because it automatically bubbles.
来源:https://stackoverflow.com/questions/43098550/react-js-broke-dispatchevent