Rob Rob - 1 year ago 788
Javascript Question

Using debouncer with React event

I have an onchange event for a field that needs to be debounced, I'm using underscore for that, however when I use the debouncer the event that is passed to the React handler appears to be out of date.

<div className='input-field'>
<input onChange={_.debounce(this.uriChangeHandler.bind(this), 500)} id='source_uri' type='text' name='source_uri' autofocus required />
<label htmlFor='source_uri'>Website Link</label>

uriChangeHandler(event) {
let uriField = $(;
let uri = uriField.val();
itemCreateError: null,
loading: true
this.loadUriMetaData(uri, uriField);

I'm getting this error:
Warning: This synthetic event is reused for performance reasons. If you're seeing this, you're calling preventDefault on a released/nullified synthetic event. This is a no-op. See for more information.

Using the onchange w/o the debouncer works fine.

Answer Source

in yout case it might help

class HelloWorldComponent extends React.Component {
  uriChangeHandler(target) {

  render() {
    var myHandler = _.compose(
      _.debounce(this.uriChangeHandler.bind(this), 5e2),'target')
    return (      
      <input onChange={myHandler}  />



Also you can use _.clone instead of'target') if you want to get the complete event object.


To prevent React nullifies the event you must call event.persist() as stated on React doc:

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.

And hence you could use e => e.persist() || e instead of _.clone JSBin

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download