Answer the question
In order to leave comments, you need to log in
Why might debounce not work?
Hey!
Why does it work without delay?
function debounce(fn, ms) {
let timeout;
return function () {
const fnCall = () => {
fn.apply(this, arguments);
};
clearTimeout(timeout);
timeout = setTimeout(fnCall, ms);
};
}
useEffect(() => {
debounce(getData(), 2000);
}, [state.query]);
function getData() {
...
Answer the question
In order to leave comments, you need to log in
debounce(getData(), 2000);
const getData = useCallback(debounce(query => {
/*
* здесь всё по-старому, кроме body: JSON.stringify({ query: state.query }),
* надо заменить на body: JSON.stringify({ query }),
*/
}, 2000), []);
useEffect(() => {
getData(state.query);
}, [ state.query ]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question