From 3c89a4032c9645df7933c78890dec1a4854b4f0c Mon Sep 17 00:00:00 2001 From: Hubert Bieszczad Date: Tue, 21 Jan 2025 08:01:08 +0100 Subject: [PATCH] fix: find native ref of complex RN components --- src/core/createUnistylesElement.native.tsx | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/core/createUnistylesElement.native.tsx b/src/core/createUnistylesElement.native.tsx index 8d0ef8d1..8bc19ae1 100644 --- a/src/core/createUnistylesElement.native.tsx +++ b/src/core/createUnistylesElement.native.tsx @@ -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(null) @@ -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)