Skip to content

Commit

Permalink
Merge pull request #510 from jpudysz/fix/findRef
Browse files Browse the repository at this point in the history
fix: find native ref of complex RN components
  • Loading branch information
jpudysz authored Jan 21, 2025
2 parents 1ed2640 + 3c89a40 commit fc9fa69
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/core/createUnistylesElement.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ import { UnistylesShadowRegistry } from '../specs'
import { passForwardedRef } from './passForwardRef'
import { maybeWarnAboutMultipleUnistyles } from './warn'

const getNativeRef = (Component: any, ref: any) => {
switch (Component.name) {
case 'KeyboardAvoidingView':
return ref.viewRef?.current
case 'FlatList':
return ref.getNativeScrollRef?.()
case 'VirtualizedList':
return ref.getScrollRef?.()
default:
return ref
}
}

export const createUnistylesElement = (Component: any) => React.forwardRef((props, forwardedRef) => {
const storedRef = useRef<unknown>(null)

Expand All @@ -20,10 +33,7 @@ export const createUnistylesElement = (Component: any) => React.forwardRef((prop
{...props}
ref={(ref: unknown) => {
if (ref) {
storedRef.current = Component.name === 'KeyboardAvoidingView'
// @ts-ignore this is special case for KeyboardAvoidingView
? ref.viewRef.current
: ref
storedRef.current = getNativeRef(Component, ref)
}

passForwardedRef(props, ref, forwardedRef)
Expand Down

0 comments on commit fc9fa69

Please sign in to comment.