Perform debounce in React.js

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

    There's a use-debounce package that you can use with ReactJS hooks.

    From package's README:

    import { useDebounce } from 'use-debounce';
    
    export default function Input() {
      const [text, setText] = useState('Hello');
      const [value] = useDebounce(text, 1000);
    
      return (
        
    { setText(e.target.value); }} />

    Actual value: {text}

    Debounce value: {value}

    ); }

    As you can see from the example above, it is set up to update the variable value only once every second (1000 milliseconds).

提交回复
热议问题