Skip to content

Commit 484a50c

Browse files
committed
fix: avoid shared value reads during render
1 parent c958062 commit 484a50c

File tree

1 file changed

+10
-4
lines changed
  • src/components/KeyboardAvoidingView

1 file changed

+10
-4
lines changed

src/components/KeyboardAvoidingView/hooks.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
import { useState } from "react";
12
import { useSharedValue } from "react-native-reanimated";
23

34
import { useKeyboardContext } from "../../context";
45
import { useKeyboardHandler } from "../../hooks";
56

67
export const useKeyboardAnimation = () => {
78
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);
1218

1319
useKeyboardHandler(
1420
{

0 commit comments

Comments
 (0)