React.js打破了dispatchEvent [英] React.js broke dispatchEvent

查看:534
本文介绍了React.js打破了dispatchEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

我在事件中添加事件处理程序:

I add event handler in react:

<div onClick={someHandler}/>

然后我调度事件:

let clickEvt = new MouseEvent('click', {
    'bubbles': false,
    'cancelable': true
});

elm.dispatchEvent(clickEvt);

但是什么也没发生.我听说您可以使用elm.click()来触发react事件.我想知道这是否是正确的反应方式? click()dispatchEvent()有什么区别?因为我有点想坚持dispatchEvent().

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

推荐答案

全部

我知道了. bubbles必须设置为true才能作出反应以接收事件,这就是click()方法起作用的原因,因为它会自动冒泡.

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.

这篇关于React.js打破了dispatchEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆