How to use throttle or debounce with React Hook?

后端 未结 17 1023
甜味超标
甜味超标 2020-11-30 04:27

I\'m trying to use the throttle method from lodash in a functional component, e.g.:

const App = () => {
  const [value, setValue         


        
17条回答
  •  孤街浪徒
    2020-11-30 05:05

    I'd like to join the party with my input debounce using useState:

    // import { React, useState } from 'react' // nomral import
    const { useState } = React // inline import
    
    const App = () => {
      const [t, setT] = useState();
      
      const handleChange = ({ target: { value } }) => {
        clearTimeout(t)
        setT(setTimeout(() => console.log(value), 700))
      }
      
      return (
        
      )
    }
    
    ReactDOM.render(, document.getElementById('root'))
    
    
    

提交回复
热议问题