Skip to content

Improve the useCombinedRefs hook by using a Proxy #2197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export {useRenderForcingRef} from './useRenderForcingRef'
export {useProvidedStateOrCreate} from './useProvidedStateOrCreate'
export {useMenuInitialFocus} from './useMenuInitialFocus'
export {useMnemonics} from './useMnemonics'
export {useCombinedRefs} from './useCombinedRefs'
64 changes: 43 additions & 21 deletions src/hooks/useCombinedRefs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
import {ForwardedRef, useRef} from 'react'
import useLayoutEffect from '../utils/useIsomorphicLayoutEffect'
import {ForwardedRef, useCallback, useEffect, useMemo, useRef} from 'react'

/**
* Ref that can perform a side effect on change while also providing access to the current
* value through `.current`.
*/
const useObservableRef = <T>(initialValue: T, onChange: (value: T) => void) => {
const onChangeRef = useRef(onChange)
onChangeRef.current = onChange

return useMemo(
() =>
new Proxy<React.MutableRefObject<T>>(
{current: initialValue},
{
set(target, prop, value) {
if (prop === 'current') {
target[prop] = value
onChangeRef.current(value)
return true
}
return false
}
}
),
// This dependency array MUST be empty because ref objects are guarunteed to be constant
// and we don't need to track initialValue
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
)
}

/**
* Creates a ref by combining multiple constituent refs. The ref returned by this hook
Expand All @@ -10,31 +39,24 @@ import useLayoutEffect from '../utils/useIsomorphicLayoutEffect'
* @param refs
*/
export function useCombinedRefs<T>(...refs: (ForwardedRef<T> | null | undefined)[]) {
const combinedRef = useRef<T | null>(null)

useLayoutEffect(() => {
function setRefs(current: T | null = null) {
const syncRefs = useCallback(
(value: T | null) => {
for (const ref of refs) {
if (!ref) {
return
}
if (!ref) continue

if (typeof ref === 'function') {
ref(current)
ref(value ?? null)
} else {
ref.current = current
ref.current = value ?? null
}
}
}
},
[refs]
)

setRefs(combinedRef.current)
const targetRef = useObservableRef<T | null>(null, syncRefs)

return () => {
// ensure the refs get updated on unmount
// eslint-disable-next-line react-hooks/exhaustive-deps
setRefs(combinedRef.current)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [...refs, combinedRef.current])
useEffect(() => syncRefs(targetRef.current), [syncRefs, targetRef])

return combinedRef
return targetRef
}