7079462503
- react 18.2.0 to 19.2.6
- react-dom 18.2.0 to 19.2.6
- @types/react 18.2.39 to 19.2.15
- @types/react-dom 18.2.17 to 19.2.3
React 19 breaking changes fixed:
- useRef<T>(null) now returns RefObject<T | null>; cast to
RefObject<T> at 16 component call sites (safe, runtime unchanged)
- useRef<T>() without arg no longer valid; add | undefined>(undefined)
in useDebounce, useFileDrop, useThrottle, useVirtualPaginator hooks,
RoomInput, RoomTimeline, and ClientNonUIFeatures
- useReducer<typeof reducer> 1-arg form removed; drop explicit type arg
in useForceUpdate (inferred from reducer function)
- global JSX namespace removed; import type { JSX } from react in
react-custom-html-parser.tsx
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
42 lines
1018 B
TypeScript
42 lines
1018 B
TypeScript
import { useCallback, useRef } from 'react';
|
|
|
|
export interface ThrottleOptions {
|
|
wait?: number;
|
|
immediate?: boolean;
|
|
}
|
|
|
|
export type ThrottleCallback<T extends unknown[]> = (...args: T) => void;
|
|
|
|
export function useThrottle<T extends unknown[]>(
|
|
callback: ThrottleCallback<T>,
|
|
options?: ThrottleOptions,
|
|
): ThrottleCallback<T> {
|
|
const timeoutIdRef = useRef<number | undefined>(undefined);
|
|
const argsRef = useRef<T | undefined>(undefined);
|
|
const { wait, immediate } = options ?? {};
|
|
|
|
const debounceCallback = useCallback(
|
|
(...cbArgs: T) => {
|
|
argsRef.current = cbArgs;
|
|
|
|
if (timeoutIdRef.current) {
|
|
return;
|
|
}
|
|
if (immediate) {
|
|
callback(...cbArgs);
|
|
}
|
|
|
|
timeoutIdRef.current = window.setTimeout(() => {
|
|
if (argsRef.current) {
|
|
callback(...argsRef.current);
|
|
}
|
|
argsRef.current = undefined;
|
|
timeoutIdRef.current = undefined;
|
|
}, wait);
|
|
},
|
|
[callback, wait, immediate],
|
|
);
|
|
|
|
return debounceCallback;
|
|
}
|