Created
December 11, 2021 04:32
-
-
Save intergalacticspacehighway/52252474872c9e2246e398179920ccf2 to your computer and use it in GitHub Desktop.
React native pager view events with reanimated
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
import React from 'react'; | |
import {View, StyleSheet, Text} from 'react-native'; | |
import PagerView from 'react-native-pager-view'; | |
import Animated, {useHandler, useEvent} from 'react-native-reanimated'; | |
const AnimatedPager = Animated.createAnimatedComponent(PagerView); | |
export function usePagerScrollHandler(handlers, dependencies) { | |
const {context, doDependenciesDiffer} = useHandler(handlers, dependencies); | |
const subscribeForEvents = ['onPageScroll']; | |
return useEvent( | |
event => { | |
'worklet'; | |
const {onPageScroll} = handlers; | |
if (onPageScroll && event.eventName.endsWith('onPageScroll')) { | |
onPageScroll(event, context); | |
} | |
}, | |
subscribeForEvents, | |
doDependenciesDiffer, | |
); | |
} | |
export default MyPager = () => { | |
const handler = usePagerScrollHandler({ | |
onPageScroll: e => { | |
'worklet'; | |
console.log(e.offset, e.position); | |
}, | |
}); | |
return ( | |
<AnimatedPager | |
style={styles.pagerView} | |
initialPage={0} | |
onPageScroll={handler}> | |
<View key="1"> | |
<Text>First page</Text> | |
</View> | |
<View key="2"> | |
<Text>Second page</Text> | |
</View> | |
</AnimatedPager> | |
); | |
}; | |
const styles = StyleSheet.create({ | |
pagerView: { | |
flex: 1, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you think it would be possible to adjust similar event handlers on TextInput events like onContentSizeChange, onLayout and similar?