-
Notifications
You must be signed in to change notification settings - Fork 2
/
useScrollY.ts
34 lines (31 loc) · 900 Bytes
/
useScrollY.ts
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
import { useState } from 'react'
import Animated from 'react-native-reanimated'
export interface ScrollYHook {
animatedScrollY: Animated.Value<number>
onScroll: any
}
/**
* This function is a hook-version of this tutorial for react-native-reanimated:
* https://callstack.com/blog/reanimating-your-react-native-experience/
*/
export default function useScrollY(): ScrollYHook {
const [value] = useState<Animated.Value<number>>(() => new Animated.Value(0))
const [onScroll] = useState(() => {
return Animated.event(
[
{
nativeEvent: {
contentOffset: {
y: value,
},
},
},
],
{ useNativeDriver: true }
)
})
return {
animatedScrollY: value,
onScroll,
}
}