使用Underscore反跳获取事件对象[反应] [英] Getting the event object with Underscore debounce[React]

查看:95
本文介绍了使用Underscore反跳获取事件对象[反应]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对已成功执行的操作使用反跳功能,但是我想将e作为参数传递,但它不起作用。有什么办法可以做到?

I am trying to use debounce on an action I have managed to do that however I want to pass e through as a parameter but it is not working. Is there any way I could do this?

 constructor(props, context) {
    super(props, context);
    this.testing = _.debounce(this.testing.bind(this), 2000);
}

 @action testing(e) {
     alert("debounced!!");
     //use e.target ... 
   }

如果我接受它会进入功能,否则不会进入。我应该怎么做才能解决这个问题?

If I take away e it will get into the function otherwise not. What should I do to resolve this?

推荐答案

您可以使用 event.persist() 将事件传递给debounce方法。

You can make use of event.persist() to pass on the event to the debounce method.

根据DOCS:


如果要访问事件属性异步方式,您
应该在事件上调用 event.persist(),这将删除
综合事件,并允许对该事件的引用由用户代码保留

If you want to access the event properties in an asynchronous way, you should call event.persist() on the event, which will remove the synthetic event from the pool and allow references to the event to be retained by user code.

可以将事件用作

constructor(props, context) {
    super(props, context);
    this.testing = _.debounce(this.testing.bind(this), 2000);
}

 @action testing(e) {
     alert("debounced!!");
     //use e.target ... 
   }
onChange = (e) => {
    e.persist();
    this.testing(e);
}

这篇关于使用Underscore反跳获取事件对象[反应]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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