File tree 1 file changed +10
-4
lines changed
src/components/KeyboardAvoidingView
1 file changed +10
-4
lines changed Original file line number Diff line number Diff line change
1
+ import { useState } from "react" ;
1
2
import { useSharedValue } from "react-native-reanimated" ;
2
3
3
4
import { useKeyboardContext } from "../../context" ;
4
5
import { useKeyboardHandler } from "../../hooks" ;
5
6
6
7
export const useKeyboardAnimation = ( ) => {
7
8
const { reanimated } = useKeyboardContext ( ) ;
8
- const heightWhenOpened = useSharedValue ( - reanimated . height . value ) ;
9
- const height = useSharedValue ( - reanimated . height . value ) ;
10
- const progress = useSharedValue ( reanimated . progress . value ) ;
11
- const isClosed = useSharedValue ( reanimated . progress . value === 0 ) ;
9
+
10
+ // calculate it only once on mount, to avoid `SharedValue` reads during a render
11
+ const [ initialHeight ] = useState ( ( ) => - reanimated . height . value ) ;
12
+ const [ initialProgress ] = useState ( ( ) => reanimated . progress . value ) ;
13
+
14
+ const heightWhenOpened = useSharedValue ( initialHeight ) ;
15
+ const height = useSharedValue ( initialHeight ) ;
16
+ const progress = useSharedValue ( initialProgress ) ;
17
+ const isClosed = useSharedValue ( initialProgress === 0 ) ;
12
18
13
19
useKeyboardHandler (
14
20
{
You can’t perform that action at this time.
0 commit comments