Skip to content

Commit

Permalink
Avoid error in logs on React <19 when using composeRefs (#3285)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitri-gb authored Dec 13, 2024
1 parent 02543ca commit 4e6e632
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/react/compose-refs/src/composeRefs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ function setRef<T>(ref: PossibleRef<T>, value: T) {
function composeRefs<T>(...refs: PossibleRef<T>[]) {
return (node: T) => {
const cleanups = refs.map((ref) => setRef(ref, node));
return () => {
cleanups.forEach((cleanup, i) => {
if (typeof cleanup == 'function') {
cleanup();
} else {
setRef(refs[i], null);
}
});
};
if (cleanups.some((c) => typeof c == 'function')) {
return () => {
cleanups.forEach((cleanup, i) => {
if (typeof cleanup == 'function') {
cleanup();
} else {
setRef(refs[i], null);
}
});
};
}
};
}

Expand Down

0 comments on commit 4e6e632

Please sign in to comment.