Skip to content

Instantly share code, notes, and snippets.

@intergalacticspacehighway
Created December 11, 2021 04:32
Show Gist options
  • Save intergalacticspacehighway/52252474872c9e2246e398179920ccf2 to your computer and use it in GitHub Desktop.
Save intergalacticspacehighway/52252474872c9e2246e398179920ccf2 to your computer and use it in GitHub Desktop.
React native pager view events with reanimated
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,
},
});
@danilofatcat
Copy link

Do you think it would be possible to adjust similar event handlers on TextInput events like onContentSizeChange, onLayout and similar?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment