I am trying to make a simple debouncing hook (implementation from a video) and use it in the Context but it doesn't work. More interestingly, it does not allow me to type anything in the input. I store handler functions and state in the Context and passing necessary things to child components.
useDebounce.js
import { useState } from 'react';
export default function useDebounce() {
const [typingTimeout, setTypingTimeout] = useState('');
function debounce(func, wait) {
clearTimeout(typingTimeout);
const timeout = setTimeout(() => {
func();
}, wait);
setTypingTimeout(timeout);
}
return debounce;
}
Context API
const [text, setText] = useState('');
const debounce = useDebounce();
const handleSearch = e => {
if (e.target.value === '' || e.target.value.match(/^[0-9]+$/)) {
debounce(() => setText(e.target.value), 1000);
}
};