diff --git a/src/__tests__/use-double-click.test.js b/src/__tests__/use-double-click.test.js
index d7c9da0..49571be 100644
--- a/src/__tests__/use-double-click.test.js
+++ b/src/__tests__/use-double-click.test.js
@@ -36,6 +36,21 @@ test('calls "onDoubleClick" callback on double click', async () => {
expect(onDoubleClick).toHaveBeenCalledTimes(1);
});
+test('calls "onDoubleClick" callback on double click despite rerender between clicks', async () => {
+ const onDoubleClick = jest.fn();
+ const { getByText, rerender } = render(
+
+ );
+
+ fireEvent.click(getByText(/Click Me/i));
+ rerender();
+ fireEvent.click(getByText(/Click Me/i));
+
+ await new Promise(r => setTimeout(r, 300));
+
+ expect(onDoubleClick).toHaveBeenCalledTimes(1);
+});
+
test('calls "onSingleClick" callback with custom latency', async () => {
const onSingleClick = jest.fn();
const latency = 150;
diff --git a/src/index.js b/src/index.js
index 5977b3f..01f8676 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,5 +1,8 @@
import { useEffect } from 'react';
+// prevent passing a new instance of noop on every render
+const noop = () => null;
+
/**
* A simple React hook for differentiating single and double clicks on the same component.
*
@@ -11,8 +14,8 @@ import { useEffect } from 'react';
const useDoubleClick = ({
ref,
latency = 300,
- onSingleClick = () => null,
- onDoubleClick = () => null
+ onSingleClick = noop,
+ onDoubleClick = noop
}) => {
useEffect(() => {
const clickRef = ref.current;
@@ -35,7 +38,7 @@ const useDoubleClick = ({
return () => {
clickRef.removeEventListener('click', handleClick);
};
- });
+ }, [latency, onDoubleClick, onSingleClick, ref]);
};
export default useDoubleClick;