refactor(react): fix a few more rules of hooks violations

This commit is contained in:
Elian Doran
2025-08-25 18:41:48 +03:00
parent 733ec2c145
commit 5a54dd666f
6 changed files with 27 additions and 18 deletions

View File

@@ -509,4 +509,16 @@ export function useLegacyImperativeHandlers(handlers: Record<string, Function>)
useEffect(() => {
Object.assign(parentComponent as never, handlers);
}, [ handlers ]);
}
export function useSyncedRef<T>(externalRef?: RefObject<T>, initialValue: T | null = null): RefObject<T> {
const ref = useRef<T>(initialValue);
useEffect(() => {
if (externalRef) {
externalRef.current = ref.current;
}
}, [ ref, externalRef ]);
return ref;
}