Skip to content

Commit aad6451

Browse files
authored
Merge pull request #108 from charlie-tango/fix/expand-options-to-deps
fix: expand opts to deps, so hook isn't recreated
2 parents 984c886 + b359950 commit aad6451

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/__tests__/useDebouncedCallback.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,12 @@ test("should infer the correct callback signature", async () => {
133133
expectTypeOf(result.current).parameter(1).toMatchTypeOf<number>();
134134
expectTypeOf(result.current).parameter(2).toMatchTypeOf<{ input: string }>();
135135
});
136+
137+
test("should not recreate the hook on each render", () => {
138+
const cb = vi.fn();
139+
const { result, rerender } = renderHook(() => useDebouncedCallback(cb, 500));
140+
const firstValue = result.current;
141+
rerender();
142+
// Validate that we didn't recreate he debounced function. It should stay the same, until the options change
143+
expect(firstValue).toBe(result.current);
144+
});

src/hooks/useDebouncedCallback.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,5 @@ export function useDebouncedCallback<
113113
};
114114

115115
return debounce;
116-
}, [wait, options]);
116+
}, [wait, options.trailing, options.leading]);
117117
}

0 commit comments

Comments
 (0)