-
Notifications
You must be signed in to change notification settings - Fork 424
/
ReanimatedOnPageScrollExample.tsx
57 lines (51 loc) · 1.38 KB
/
ReanimatedOnPageScrollExample.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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: any, dependencies?: any) {
const { context, doDependenciesDiffer } = useHandler(handlers, dependencies);
const subscribeForEvents = ['onPageScroll'];
return useEvent<any>(
(event) => {
'worklet';
const { onPageScroll } = handlers;
if (onPageScroll && event.eventName.endsWith('onPageScroll')) {
onPageScroll(event, context);
}
},
subscribeForEvents,
doDependenciesDiffer
);
}
export default () => {
const handler = usePagerScrollHandler({
onPageScroll: (e: any) => {
'worklet';
console.log(e.offset, e.position);
},
});
return (
<AnimatedPager
testID={'pager-view'}
style={styles.pagerView}
initialPage={0}
onPageScroll={handler}
>
<View testID={'1'} key="1">
<Text>First page</Text>
</View>
<View testID={'2'} key="2">
<Text>Second page</Text>
</View>
<View testID={'3'} key="3">
<Text>Third page</Text>
</View>
</AnimatedPager>
);
};
const styles = StyleSheet.create({
pagerView: {
flex: 1,
},
});