Skip to content

Commit 58a820f

Browse files
docs: Fix measure InteracitveExample (#6452)
The box in InteractiveExample was disappearing form time to time. Usage of animatedProps fixed that issues. Before: https://github.com/user-attachments/assets/1cef8cda-0d8e-44f6-a377-ee5a3889ff6a After: https://github.com/user-attachments/assets/204e9a25-fd6f-4440-9c5d-52e11fe44cc9
1 parent d186f86 commit 58a820f

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

packages/docs-reanimated/src/examples/MeasureBasic.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import React from 'react';
2-
import { Button, StyleSheet, View, Text } from 'react-native';
2+
import { Button, StyleSheet, View, TextInput } from 'react-native';
33
import Animated, {
44
MeasuredDimensions,
55
measure,
6-
runOnJS,
6+
useAnimatedProps,
77
useAnimatedRef,
88
useSharedValue,
99
withTiming,
1010
} from 'react-native-reanimated';
1111

12+
const AnimatedTextInput = Animated.createAnimatedComponent(TextInput);
13+
1214
export default function App() {
1315
const animatedRef = useAnimatedRef<Animated.View>();
1416
const width = useSharedValue<number>(100);
15-
const [text, setText] = React.useState(width.value);
17+
const text = useSharedValue(100);
1618

1719
const handlePress = () => {
1820
width.value = withTiming(width.value + 50, {}, () => {
@@ -23,14 +25,21 @@ export default function App() {
2325
return;
2426
}
2527

26-
runOnJS(setText)(Math.floor(measurement.width));
28+
text.value = Math.floor(measurement.width);
2729
});
2830
};
2931

32+
const animatedProps = useAnimatedProps(() => {
33+
return {
34+
text: `width: ${text.value}`,
35+
defaultValue: `width: ${text.value}`,
36+
};
37+
});
38+
3039
return (
3140
<View style={styles.container}>
3241
<Animated.View ref={animatedRef} style={{ ...styles.box, width }} />
33-
<Text style={styles.label}>width: {text}</Text>
42+
<AnimatedTextInput animatedProps={animatedProps} style={styles.label} />
3443
<Button onPress={handlePress} title="Click me" />
3544
</View>
3645
);
@@ -50,5 +59,6 @@ const styles = StyleSheet.create({
5059
fontSize: 24,
5160
marginVertical: 16,
5261
color: '#b58df1',
62+
textAlign: 'center',
5363
},
5464
});

0 commit comments

Comments
 (0)