Skip to content

Commit

Permalink
Merge pull request #108 from charlie-tango/fix/expand-options-to-deps
Browse files Browse the repository at this point in the history
fix: expand opts to deps, so hook isn't recreated
  • Loading branch information
thebuilder authored Aug 13, 2024
2 parents 984c886 + b359950 commit aad6451
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/__tests__/useDebouncedCallback.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,12 @@ test("should infer the correct callback signature", async () => {
expectTypeOf(result.current).parameter(1).toMatchTypeOf<number>();
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);
});
2 changes: 1 addition & 1 deletion src/hooks/useDebouncedCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,5 @@ export function useDebouncedCallback<
};

return debounce;
}, [wait, options]);
}, [wait, options.trailing, options.leading]);
}

0 comments on commit aad6451

Please sign in to comment.