I'm trying to wait 2 seconds when an input text contains text and make a request to my API. I wrote this function as a react hook called useBounce:
const useDebounce = (func, milliseconds) => {
const time = milliseconds || 400
let timer
return event => {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(func, time, event)
}
}
export default useDebounce
So I have an input with an onChange that when I try to write react it doesn't allow me to do it and the call function to my API doesn't execute:
const searchDebounce = useDebounce((e) => {
setInput(e.target.value);
e.preventDefault()
console.log(e.target.value)
}, 2000)
return (
<>
<input
type="text"
onChange={ searchDebounce }
value={input}
className='border-2 border-cyan-500'
/>
</>
)
For now I am trying to see what is written in the input through the console but since it does not allow me to write, the console is blank.
Thanks for your help.