Perform debounce in React.js

前端 未结 30 2204
礼貌的吻别
礼貌的吻别 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:53

    Using ES6 CLASS and React 15.x.x & lodash.debounce Im using React's refs here since event losses the this bind internally.

    class UserInput extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          userInput: ""
        };
        this.updateInput = _.debounce(this.updateInput, 500);
      }
    
    
      updateInput(userInput) {
        this.setState({
          userInput
        });
        //OrderActions.updateValue(userInput);//do some server stuff
      }
    
    
      render() {
        return ( 

    User typed: { this.state.userInput }

    this.updateInput(this.refs.userValue.value) } type = "text" / >
    ); } } ReactDOM.render( < UserInput / > , document.getElementById('root') );
    
    
    
    
    
    

提交回复
热议问题