Perform debounce in React.js

前端 未结 30 1563
礼貌的吻别
礼貌的吻别 2020-11-22 04:11

How do you perform debounce in React.js?

I want to debounce the handleOnChange.

I tried with debounce(this.handleOnChange, 200) but it doesn\'t

30条回答
  •  无人及你
    2020-11-22 04:44

    If all you need from the event object is to get the DOM input element, the solution is much simpler – just use ref. Note that this requires Underscore:

    class Item extends React.Component {
        constructor(props) {
            super(props);
            this.saveTitle = _.throttle(this.saveTitle.bind(this), 1000);
        }
        saveTitle(){
            let val = this.inputTitle.value;
            // make the ajax call
        }
        render() {
            return  this.inputTitle = el } 
                        type="text" 
                        defaultValue={this.props.title} 
                        onChange={this.saveTitle} />
        }
    }
    

提交回复
热议问题