1
1
import React from 'react' ;
2
- import { Button , StyleSheet , View , Text } from 'react-native' ;
2
+ import { Button , StyleSheet , View , TextInput } from 'react-native' ;
3
3
import Animated , {
4
4
MeasuredDimensions ,
5
5
measure ,
6
- runOnJS ,
6
+ useAnimatedProps ,
7
7
useAnimatedRef ,
8
8
useSharedValue ,
9
9
withTiming ,
10
10
} from 'react-native-reanimated' ;
11
11
12
+ const AnimatedTextInput = Animated . createAnimatedComponent ( TextInput ) ;
13
+
12
14
export default function App ( ) {
13
15
const animatedRef = useAnimatedRef < Animated . View > ( ) ;
14
16
const width = useSharedValue < number > ( 100 ) ;
15
- const [ text , setText ] = React . useState ( width . value ) ;
17
+ const text = useSharedValue ( 100 ) ;
16
18
17
19
const handlePress = ( ) => {
18
20
width . value = withTiming ( width . value + 50 , { } , ( ) => {
@@ -23,14 +25,21 @@ export default function App() {
23
25
return ;
24
26
}
25
27
26
- runOnJS ( setText ) ( Math . floor ( measurement . width ) ) ;
28
+ text . value = Math . floor ( measurement . width ) ;
27
29
} ) ;
28
30
} ;
29
31
32
+ const animatedProps = useAnimatedProps ( ( ) => {
33
+ return {
34
+ text : `width: ${ text . value } ` ,
35
+ defaultValue : `width: ${ text . value } ` ,
36
+ } ;
37
+ } ) ;
38
+
30
39
return (
31
40
< View style = { styles . container } >
32
41
< Animated . View ref = { animatedRef } style = { { ...styles . box , width } } />
33
- < Text style = { styles . label } > width: { text } </ Text >
42
+ < AnimatedTextInput animatedProps = { animatedProps } style = { styles . label } / >
34
43
< Button onPress = { handlePress } title = "Click me" />
35
44
</ View >
36
45
) ;
@@ -50,5 +59,6 @@ const styles = StyleSheet.create({
50
59
fontSize : 24 ,
51
60
marginVertical : 16 ,
52
61
color : '#b58df1' ,
62
+ textAlign : 'center' ,
53
63
} ,
54
64
} ) ;
0 commit comments