Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Fixes #4376. Analogous to #3988. Solution: copy the code from `createAnimatedComponent.tsx` to `useAnimatedRef.ts` https://github.com/software-mansion/react-native-reanimated/blob/87fb9c6a1fb6b8c839b65b90ad16f574f45a1294/src/createAnimatedComponent.tsx#L289-L291 Example: <details> <summary>App.tsx</summary> ```tsx import Animated, { runOnUI, scrollTo, useAnimatedRef, } from 'react-native-reanimated'; import {Button, StyleSheet, Switch, Text, View} from 'react-native'; import {FlashList} from '@shopify/flash-list'; import React from 'react'; declare const _WORKLET: boolean; const AnimatedFlashList = Animated.createAnimatedComponent(FlashList); interface Item { label: string; } const DATA: Item[] = [...Array(100)].map((_, i) => ({label: String(i)})); function getRandomOffset() { 'worklet'; return Math.random() * 2000; } export default function ScrollToExample() { const [animated, setAnimated] = React.useState(true); const aref = useAnimatedRef<FlashList<Item>>(); const scrollFromJS = () => { console.log(_WORKLET); aref.current?.scrollToOffset({offset: getRandomOffset(), animated}); }; const scrollFromUI = () => { runOnUI(() => { 'worklet'; console.log(_WORKLET); scrollTo(aref, 0, getRandomOffset(), animated); })(); }; return ( <> <View style={styles.buttons}> <Switch value={animated} onValueChange={setAnimated} style={styles.switch} /> <Button onPress={scrollFromJS} title="Scroll from JS" /> <Button onPress={scrollFromUI} title="Scroll from UI" /> </View> <AnimatedFlashList // @ts-ignore createAnimatedComponent seems to break generic types ref={aref} data={DATA} // @ts-ignore createAnimatedComponent seems to break generic types renderItem={({item}: {item: Item}) => ( <Text key={item.label} style={styles.text}> {item.label} </Text> )} estimatedItemSize={200} /> </> ); } const styles = StyleSheet.create({ switch: { marginBottom: 10, }, buttons: { marginTop: 80, marginBottom: 40, alignItems: 'center', }, text: { fontSize: 50, textAlign: 'center', }, }); ``` </details> Before: <img src="https://user-images.githubusercontent.com/20516055/233323545-427cc3d4-4479-4b02-a24b-5a3039fb3105.png" width="300" /> Android: https://user-images.githubusercontent.com/20516055/233322746-637fa2a9-0e93-40ac-baa8-c3c3d2449867.mp4 iOS: https://user-images.githubusercontent.com/20516055/233322714-20c03ade-1f15-4d76-8a5e-b4b59ebe8a73.mp4 ## Test plan 1. Install FlashList in Example app with `yarn add @shopify/flash-list && cd ios && pod install` 2. Paste the attached code snippet into App.tsx > **Warning** > It looks like animated refs don't update properly on render. In this case, pressing "Scroll from UI" button after a fast refresh does nothing. Please reload the <kbd>r</kbd> to reload the app and check again.
- Loading branch information