2

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().

BigName
  • 1,018
  • 2
  • 14
  • 29
  • 1
    What is the value of `elm`? – Felix Kling Mar 29 '17 at 16:00
  • It is a HTMLnode I retrieve by using getElementsByTagName('button')[idx], the button should exist, otherwise it should say somthing like elm is undefined. – BigName Mar 29 '17 at 16:06
  • @felix I just double checked, `elm` is logged as the right button before `dispatchEvent` is called. When I click the button with my mouse, everything works fine, but `dispatchEvent` does nothing. Sorry I used `div` in example, but it's a button. – BigName Mar 29 '17 at 17:04

1 Answers1

0

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.

BigName
  • 1,018
  • 2
  • 14
  • 29