Skip to content

Instantly share code, notes, and snippets.

@guamacherox
Last active March 31, 2021 01:17
Show Gist options
  • Save guamacherox/c699a1fb297a516fe9e33f9abcd4a273 to your computer and use it in GitHub Desktop.
Save guamacherox/c699a1fb297a516fe9e33f9abcd4a273 to your computer and use it in GitHub Desktop.
React hook for handling debounce using lodash debounce
import { useRef } from 'react';
import debounce from 'lodash/debounce';
import PropTypes from 'prop-types';
function useDebounce(customFunction, t = 500) {
const debouncedFunction = useRef(debounce(customFunction, t)).current;
return debouncedFunction;
}
useDebounce.propTypes = {
customFunction: PropTypes.func.isRequired,
t: PropTypes.number
};
export default useDebounce;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment