-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correct refs adding and removing
- Loading branch information
Showing
6 changed files
with
9,560 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ import { assignRef } from './assignRef'; | |
import { ReactRef } from './types'; | ||
import { useCallbackRef } from './useRef'; | ||
|
||
const currentValues = new WeakMap<any, ReactRef<any>[]>(); | ||
|
||
/** | ||
* Merges two or more refs together providing a single interface to set their value | ||
* @param {RefObject|Ref} refs | ||
|
@@ -19,5 +21,34 @@ import { useCallbackRef } from './useRef'; | |
* } | ||
*/ | ||
export function useMergeRefs<T>(refs: ReactRef<T>[], defaultValue?: T): React.MutableRefObject<T | null> { | ||
return useCallbackRef<T>(defaultValue || null, (newValue) => refs.forEach((ref) => assignRef(ref, newValue))); | ||
const callbackRef = useCallbackRef<T>(defaultValue || null, (newValue) => | ||
refs.forEach((ref) => assignRef(ref, newValue)) | ||
); | ||
|
||
// handle refs changes - added or removed | ||
React.useLayoutEffect(() => { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
theKashey
Author
Owner
|
||
const oldValue = currentValues.get(callbackRef); | ||
|
||
if (oldValue) { | ||
const prevRefs = new Set(oldValue); | ||
const nextRefs = new Set(refs); | ||
const current = callbackRef.current; | ||
|
||
prevRefs.forEach((ref) => { | ||
if (!nextRefs.has(ref)) { | ||
assignRef(ref, null); | ||
} | ||
}); | ||
|
||
nextRefs.forEach((ref) => { | ||
if (!prevRefs.has(ref)) { | ||
assignRef(ref, current); | ||
} | ||
}); | ||
} | ||
|
||
currentValues.set(callbackRef, refs); | ||
}, [refs]); | ||
|
||
return callbackRef; | ||
} |
Oops, something went wrong.
Thank you for this very useful library! After a recent update I noticed that since this part of the code is using
React.useLayoutEffect
, rendering the component during SSR results in the warning:This is typically worked around by something like:
Would you be open to such a change? I realize the warning is only a warning and most likely doesn't matter, but it's still annoying. Reference: https://gist.github.com/gaearon/e7d97cdf38a2907924ea12e4ebdf3c85
EDIT: Sorry, by component I was referring to
react-remove-scroll
which internally uses this.