diff --git a/src/__tests__/useDebouncedCallback.test.tsx b/src/__tests__/useDebouncedCallback.test.tsx index 48b441f..511d118 100644 --- a/src/__tests__/useDebouncedCallback.test.tsx +++ b/src/__tests__/useDebouncedCallback.test.tsx @@ -133,3 +133,12 @@ test("should infer the correct callback signature", async () => { expectTypeOf(result.current).parameter(1).toMatchTypeOf(); expectTypeOf(result.current).parameter(2).toMatchTypeOf<{ input: string }>(); }); + +test("should not recreate the hook on each render", () => { + const cb = vi.fn(); + const { result, rerender } = renderHook(() => useDebouncedCallback(cb, 500)); + const firstValue = result.current; + rerender(); + // Validate that we didn't recreate he debounced function. It should stay the same, until the options change + expect(firstValue).toBe(result.current); +}); diff --git a/src/hooks/useDebouncedCallback.ts b/src/hooks/useDebouncedCallback.ts index 25c81e0..b8058fb 100644 --- a/src/hooks/useDebouncedCallback.ts +++ b/src/hooks/useDebouncedCallback.ts @@ -113,5 +113,5 @@ export function useDebouncedCallback< }; return debounce; - }, [wait, options]); + }, [wait, options.trailing, options.leading]); }