L
L
lexstile2021-04-13 14:44:38
React
lexstile, 2021-04-13 14:44:38

How to properly combine debounce and cancel token?

There is an option with cancel token that works:

useEffect(() => {
    const asyncEnrollment = async () => {
      const data = { userId, filter };
      setLoading(true);
      await initialEnrollment(dispatch, data);
      setLoading(false);
    };

    asyncEnrollment();
  }, [dispatch, userId, filter]);

There is a version with debounce, which also works, but cancel token does not work:
const loadEnrollment = useRef(
  debounce(async (data) => {
    setLoading(true);
    await initialEnrollment(dispatch, data);
    setLoading(false);
  }, 4000)
);

useEffect(() => {
  const data = { userId, filter };
  loadEnrollment.current(data);
}, [userId, filter]);

Separately they work, together they don't.
How to combine them?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question