-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix useScrollViewOffset taking wrong viewTag #5790
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Does it work on Fabric? If yes, should we add a separate method |
I forgor about Fabric |
tomekzaw
approved these changes
Apr 3, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i trust you
github-merge-queue bot
pushed a commit
that referenced
this pull request
Apr 3, 2024
## Summary Inspired by #5790, it seems like scrolls can be wrapped in other views (by for example setting their `onRefresh` property), which makes refs to them point to the wrapping view, not them. Thus I add support for such situations to the `useScrollViewOffset` hook, which is our only hook that needs to access the component itself. ## Test plan <details> <summary> Testing code </summary> ```tsx import React from 'react'; import { Text, Pressable, SafeAreaView, StyleSheet, Button, findNodeHandle, } from 'react-native'; import Animated, { useAnimatedRef, useScrollViewOffset, } from 'react-native-reanimated'; export default function App() { const animatedRef = useAnimatedRef<Animated.ScrollView>(); const offset = useScrollViewOffset(animatedRef); const [refreshing, setRefreshing] = React.useState(false); function printOffset() { console.log('OFFSET VALUE'); console.log(offset.value); } return ( <SafeAreaView> <Button title="refresh" onPress={() => setRefreshing((oldState) => !oldState)} /> <List animatedRef={animatedRef} printOffset={printOffset} refreshing={refreshing} /> </SafeAreaView> ); } const List = ({ animatedRef, printOffset, refreshing }) => { console.log('tag', findNodeHandle(animatedRef.current)); return ( <Animated.FlatList ref={animatedRef} refreshing={refreshing} style={{height: 600}} onRefresh={() => console.log('refreshing')} onViewableItemsChanged={() => { printOffset(); }} data={[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]} renderItem={({ _, index }) => <Item onPress={printOffset} id={index} />} /> ); }; const Item = ({ onPress, id }) => { return ( <Pressable onPress={onPress} style={styles.pressable}> <Text>{id}</Text> </Pressable> ); }; const styles = StyleSheet.create({ pressable: { padding: 40, justifyContent: 'center', alignItems: 'center', borderWidth: 1, borderColor: 'red', backgroundColor: 'white', }, }); ``` </details>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5777
Currently
useScrollViewOffset
takes the viewTag to the underlying ScrollView by callingfindNodeHandle
itself on animatedRef. This doesn't take into consideration a ScrollView that's wrapped in another view. When you specifyonRefresh
property that's what happens - ScrollView becomes a child of another view.useAnimatedRef
takes the correct tag because it uses in this casegetScrollableNode
member function of the component - but this logic isn't transferred to other hooks that require the tag.This PR adds a getter function to the object returned by
useAnimatedRef
, so no function needing the tag should ever acquire it by itself - instead it should use the getter method.Test plan
iOS
Android
Fabric
Testing code