-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrollComponent.js
72 lines (64 loc) · 1.73 KB
/
ScrollComponent.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React, {forwardRef, Fragment, useMemo, useState} from 'react';
import {ScrollView as RNScrollView, Text, View} from 'react-native';
import Animated, {
runOnJS,
useAnimatedScrollHandler,
} from 'react-native-reanimated';
const ScrollViewWrapped = forwardRef((props, ref) => {
return (
<View style={{flex: 1}}>
<RNScrollView {...props} ref={ref} />
</View>
);
});
const ScrollView = forwardRef((props, ref) => (
<RNScrollView {...props} ref={ref} />
));
const AnimatedScrollViewWrapped =
Animated.createAnimatedComponent(ScrollViewWrapped);
const AnimatedScrollView = Animated.createAnimatedComponent(ScrollView);
export default function ScrollComponent({isWrapped}) {
const [scrollValue, setScrollValue] = useState(0);
const scrollHandler = useAnimatedScrollHandler({
onScroll: ({contentOffset}) => runOnJS(setScrollValue)(contentOffset.y),
});
const items = useMemo(
() =>
Array.from({length: 30}).map((_, index) => (
<View
key={index}
style={{
height: 100,
width: '100%',
backgroundColor: 'white',
marginBottom: 20,
}}
/>
)),
[],
);
if (isWrapped) {
return (
<>
<Text>{scrollValue}</Text>
<AnimatedScrollViewWrapped
onScroll={scrollHandler}
scrollEventThrottle={16}
style={{backgroundColor: 'red', padding: 10}}>
{items}
</AnimatedScrollViewWrapped>
</>
);
}
return (
<>
<Text>{scrollValue}</Text>
<AnimatedScrollView
onScroll={scrollHandler}
scrollEventThrottle={16}
style={{backgroundColor: 'blue', padding: 10}}>
{items}
</AnimatedScrollView>
</>
);
}