簡體   English   中英

如何將 ref 傳遞給 react-native-reanimated 組件?

[英]How can I pass a ref to a react-native-reanimated component?

我正在嘗試使用與以下代碼塊類似的方法將 ref 傳遞給組件,但current值始終返回undefined 這種方法適用於來自 react-native 的普通FlatList ,但是一旦我使用Animated.FlatListAnimated.createAnimatedComponent(FlatList)

const Parent = () => {
    const flatListRef = useRef();

    useEffect(() => {
        console.log(flatListRef.current) // undefined
    })

    return (
        <View>
            <Child ref={flatListRef} />
        </View>
    )
}

const Child = React.forwardRef((props, ref) => {

    return (
        <Animated.FlatList
            ref={ref}
        />
    )
})

react-native-reanimatedreact-native-animated相比有點不同。

如果我們通過Animated.createAnimatedComponent(FlatList)創建動畫組件,那么一切都會按預期進行。

這是您的代碼的工作版本。 為了測試目的,我已經記錄了FlatList ref 的 function scrollToIndex

import Animated from "react-native-reanimated"

const ReanimatedFlatList = Animated.createAnimatedComponent(FlatList);

const Parent = (props) => {
    const flatListRef = useRef(null);

    useEffect(() => {
      console.log(flatListRef.current.scrollToIndex)
    }, [])

    return (
        <View>
            <Child ref={flatListRef} />
        </View>
    )
}

const Child = React.forwardRef((props, ref) => {
    return (
        <ReanimatedFlatList
            ref={ref}
        />
    )
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM