简体   繁体   中英

React Component - debounce

Trying to create a delay on react component that has input field that updates on change

Here is my onChange method

handleOrderQtyKeyPress (e) {
    var regex = /[^0-9]/
    if (e.key.match(regex)) {
        e.preventDefault();
    }
    if (this.state.orderQtyValue.toString().length == 3) {
        e.preventDefault();
    }
}

and the react-bootstrap component:

 <FormControl
   type='number'
   min='0'
   value={this.state.orderQtyValue}
   onChange={this.handleOrderQtyChange}
   onKeyPress={this.handleOrderQtyKeyPress}
   style={styles.orderQtyValue}
  />

so I tried importing lodash _.debounce and applying at the constructor

import debounce from 'lodash/debounce';


this.handleOrderQtyKeyPress = _.debounce(this.handleOrderQtyKeyPress.bind(this),1000);

I am not getting a debounce. What am I missing here?

I see that you use this , so I assume that FormControl is inside of a render function of your stateful component. In this case make sure that your binding and debouncing is happening in constructor of this stateful component.

```

const Component extends React.Component {
   constructor(props) {
     super(props);
     this.handleOrderQtyKeyPress = _.debounce(this.handleOrderQtyKeyPress.bind(this), 1000);
   }
}

```

Please, read comment which explains how this works

class Input extends Component {
    static propTypes = {
        onChange: PropTypes.func.isRequired,
        value: React.PropTypes.oneOfType([
            React.PropTypes.string,
            React.PropTypes.number,
        ]),
    }

    state = {
        value: '',
    }

    // When component receives updated `value` from outside, update internal `value` state.
    componentWillReceiveProps(nextProps) {
        this.setState({ value: nextProps.value });
    }

    // Store updated value localy.
    onChange = (event) => {
        this.setState({ value: event.target.value });
    }

    onBlur = (event) => {
        // Trigger change to external env.
        this.props.onChange(this.state.value);
        // Also, don't forget to call `onBlur` if received from parent.
        if (this.props.onBlur) {
            this.props.onBlur(event);
        }
    }

    render() {
        return <input {...this.props} value={this.state.value} onChange={this.onChange} onBlur={this.onBlur} />
    }
}

If you want to automatically debounce (or throttle ) a component easily, when the props change often (as opposed to internal state changed),

I've authored a tiny wrapper ( 1kb minified) for that, called React-Bouncer :

import bouncer from '@yaireo/react-bouncer'

// uses 300ms `debounce` by default
const DebouncedYourComponent = bouncer(YourComponent)

This is useful when you do not have much of a control on the rate which the props sent to the component are updated, or the root cause of the often updates is unknown.

(Obviously, using debounce on the root-cause is the first thing to try)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM