'What is the advantage of using useDebounce insted of using just debounce function

I have noticed people using this kind of useDebounced hook:

import { useState, useEffect } from 'react';

export default function useDebounce(value: any, delay: number) {

    const [debouncedValue, setDebouncedValue] = useState(value);

    useEffect(
        () => {
            const handler = setTimeout(() => {
                setDebouncedValue(value);
            }, delay);

            return () => {
                clearTimeout(handler);
            };
        }
    );

    return debouncedValue;
}

*Taken from https://dev.to/gabe_ragland/debouncing-with-react-hooks-jci

What is the advantage of using the hook, and not just simple javascript debounce, like in lodash library??



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source