software-mansion / react-native-reanimated

React Native's Animated library reimplemented
https://docs.swmansion.com/react-native-reanimated/
MIT License
8.67k stars 1.27k forks source link

Make `useScrollviewOffset` ref nullable and simplify its code #6009

Closed szydlovsky closed 2 months ago

szydlovsky commented 2 months ago

Summary

https://github.com/software-mansion/react-native-reanimated/pull/5942 needs useScrollViewOffset's ref to be nullable - hence the PR. I also checked and a lot of cleanup code in the hook was redundant.

Test plan

You can run the following code, it checks both switching between nullable and non-nullable ref, as well as two different scrolls: (add some logs in hook's registering/unregistering to see that cleanups work fine)

Code ``` TYPESCRIPT import React from 'react'; import Animated, { useAnimatedRef, useDerivedValue, useScrollViewOffset, } from 'react-native-reanimated'; import { Button, StyleSheet, Text, View } from 'react-native'; export default function EmptyExample() { const [refConnected, setRefConnected] = React.useState(false); const [refSwitched, setRefSwitched] = React.useState(false); const aref = useAnimatedRef(); const bref = useAnimatedRef(); const scrollHandler = useScrollViewOffset( refConnected ? (refSwitched ? bref : aref) : null ); useDerivedValue(() => { console.log(scrollHandler.value); }); const onButtonPress = () => { setRefConnected(!refConnected); }; const onSwitchButtonPress = () => { setRefSwitched(!refSwitched); }; return ( <> Test