使用 Underscore debounce[React] 获取事件对象

Getting the event object with Underscore debounce[React]

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

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

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

如果我去掉 e 它将进入函数,否则不会。我应该怎么做才能解决这个问题?

赋值后this.testing,

this.testing = _.debounce(this.testing.bind(this), 2000);

在某些情况下,您应该使用参数

调用该方法
onSumbit(e){
    this.testing(e);
}

像这样

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

根据文档:

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);
}