Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function useDebouncedCallback(options: Object): Function;
:nested="[
{
name: 'options.onChange',
type: 'Function',
type: '(newValue: T) => void',
required: true,
description: '디바운스할 콜백 함수예요.',
},
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useDebouncedCallback/useDebouncedCallback.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function useDebouncedCallback(options: Object): Function;
:nested="[
{
name: 'options.onChange',
type: 'Function',
type: '(newValue: T) => void',
required: true,
description: 'The callback function to debounce.',
},
Expand Down
11 changes: 7 additions & 4 deletions src/hooks/useDebouncedCallback/useDebouncedCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@ type DebounceOptions = {
* return <input type="text" onChange={(e) => debouncedSetQuery(e.target.value)} />;
* }
*/
export function useDebouncedCallback({
export function useDebouncedCallback<T>({
onChange,
timeThreshold,
leading = false,
trailing = true,
}: DebounceOptions & {
onChange: (newValue: boolean) => void;
onChange: (newValue: T) => void;
timeThreshold: number;
}) {
const handleChange = usePreservedCallback(onChange);
const ref = useRef({ value: false, clearPreviousDebounce: () => {} });
const ref = useRef<{ value: T | null; clearPreviousDebounce: () => void }>({
value: null,
clearPreviousDebounce: () => {},
});

useEffect(() => {
const current = ref.current;
Expand All @@ -63,7 +66,7 @@ export function useDebouncedCallback({
}, [leading, trailing]);

return useCallback(
(nextValue: boolean) => {
(nextValue: T) => {
if (nextValue === ref.current.value) {
Copy link

Copilot AI Sep 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The strict equality comparison may not work correctly for object types. When T is an object, this comparison will check reference equality rather than value equality, potentially causing the debounce logic to fail. Consider using a deep equality check or allowing callers to provide a custom comparison function.

Copilot uses AI. Check for mistakes.
return;
}
Expand Down
Loading