Skip to content

Commit c270546

Browse files
lunaleapsfacebook-github-bot
authored andcommitted
useAnimatedValue in react-native-github
Summary: Changelog: [Internal] - Migrated 8 files in `xplat/js/react-native-github/packages/rn-tester/` from the manual `useRef(new Animated.Value(x))` pattern to the `useAnimatedValue(x)` hook. Reviewed By: fabriziocucci Differential Revision: D93258352
1 parent 7146c13 commit c270546

File tree

8 files changed

+59
-35
lines changed

8 files changed

+59
-35
lines changed

packages/rn-tester/js/examples/Animated/EasingExample.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
StyleSheet,
2424
Text,
2525
View,
26+
useAnimatedValue,
2627
} from 'react-native';
2728

2829
type Props = Readonly<{}>;
@@ -87,9 +88,9 @@ function EasingItem({
8788
item: EasingListItem,
8889
useNativeDriver: boolean,
8990
}): React.Node {
90-
const opacityAndScale = useRef(new Animated.Value(1));
91+
const opacityAndScale = useAnimatedValue(1);
9192
const animation = useRef(
92-
Animated.timing(opacityAndScale.current, {
93+
Animated.timing(opacityAndScale, {
9394
toValue: 1,
9495
duration: 1200,
9596
easing: item.easing,
@@ -100,8 +101,8 @@ function EasingItem({
100101
const animatedStyles = [
101102
styles.box,
102103
{
103-
opacity: opacityAndScale.current,
104-
transform: [{scale: opacityAndScale.current}],
104+
opacity: opacityAndScale,
105+
transform: [{scale: opacityAndScale}],
105106
},
106107
];
107108

@@ -115,7 +116,7 @@ function EasingItem({
115116
</Text>
116117
<RNTesterButton
117118
onPress={() => {
118-
opacityAndScale.current.setValue(0);
119+
opacityAndScale.setValue(0);
119120
animation.current.start();
120121
}}>
121122
Animate

packages/rn-tester/js/examples/Animated/MovingBoxExample.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import RNTConfigurationBlock from '../../components/RNTConfigurationBlock';
1414
import RNTesterButton from '../../components/RNTesterButton';
1515
import ToggleNativeDriver from './utils/ToggleNativeDriver';
1616
import * as React from 'react';
17-
import {useRef, useState} from 'react';
18-
import {Animated, StyleSheet, Text, View} from 'react-native';
17+
import {useState} from 'react';
18+
import {Animated, StyleSheet, Text, View, useAnimatedValue} from 'react-native';
1919

2020
const containerWidth = 200;
2121
const boxSize = 50;
@@ -59,12 +59,12 @@ const styles = StyleSheet.create({
5959
type Props = Readonly<{}>;
6060

6161
function MovingBoxView({useNativeDriver}: {useNativeDriver: boolean}) {
62-
const x = useRef(new Animated.Value(0));
62+
const x = useAnimatedValue(0);
6363
const [update, setUpdate] = useState(0);
6464
const [boxVisible, setBoxVisible] = useState(true);
6565

6666
const moveTo = (pos: number) => {
67-
Animated.timing(x.current, {
67+
Animated.timing(x, {
6868
toValue: pos,
6969
duration: 1000,
7070
useNativeDriver,
@@ -76,7 +76,7 @@ function MovingBoxView({useNativeDriver}: {useNativeDriver: boolean}) {
7676
};
7777
const toggleText = boxVisible ? 'Hide' : 'Show';
7878
const onReset = () => {
79-
x.current.resetAnimation();
79+
x.resetAnimation();
8080
setUpdate(update + 1);
8181
};
8282
return (
@@ -85,11 +85,7 @@ function MovingBoxView({useNativeDriver}: {useNativeDriver: boolean}) {
8585
{boxVisible ? (
8686
<Animated.View
8787
testID="moving-view"
88-
style={[
89-
styles.content,
90-
styles.box,
91-
{transform: [{translateX: x.current}]},
92-
]}
88+
style={[styles.content, styles.box, {transform: [{translateX: x}]}]}
9389
/>
9490
) : (
9591
<Text>The box view is not being rendered</Text>

packages/rn-tester/js/examples/Animated/PressabilityWithNativeDrivers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
1212

1313
import * as React from 'react';
14-
import {useRef, useState} from 'react';
15-
import {Animated, Button, Text, View} from 'react-native';
14+
import {useState} from 'react';
15+
import {Animated, Button, Text, View, useAnimatedValue} from 'react-native';
1616

1717
const componentList: number[] = Array.from({length: 100}, (_, i) => i + 1);
1818

1919
function PressableWithNativeDriver() {
20-
const currScroll = useRef(new Animated.Value(0)).current;
20+
const currScroll = useAnimatedValue(0);
2121
const [count, setCount] = useState(0);
2222

2323
return (

packages/rn-tester/js/examples/AnimatedGratuitousApp/AnExSet.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@ import AnExBobble from './AnExBobble';
1414
import AnExChained from './AnExChained';
1515
import AnExScroll from './AnExScroll';
1616
import AnExTilt from './AnExTilt';
17-
import React, {useRef, useState} from 'react';
18-
import {Animated, PanResponder, StyleSheet, Text, View} from 'react-native';
17+
import React, {useState} from 'react';
18+
import {
19+
Animated,
20+
PanResponder,
21+
StyleSheet,
22+
Text,
23+
View,
24+
useAnimatedValue,
25+
} from 'react-native';
1926

2027
const randColor = () => {
2128
const colors = [0, 1, 2].map(() => Math.floor(Math.random() * 150 + 100));
@@ -39,7 +46,7 @@ const AnExSet = ({
3946
}: AnExSetProps): React.Node => {
4047
const [closeColor] = useState(randColor());
4148
const [openColor] = useState(randColor());
42-
const dismissY = useRef(new Animated.Value(0)).current;
49+
const dismissY = useAnimatedValue(0);
4350

4451
const dismissResponder = PanResponder.create({
4552
onStartShouldSetPanResponder: () => isActive,

packages/rn-tester/js/examples/AnimatedGratuitousApp/AnExTilt.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010

1111
'use strict';
1212

13-
import React, {useCallback, useEffect, useRef} from 'react';
14-
import {Animated, PanResponder, StyleSheet} from 'react-native';
13+
import React, {useCallback, useEffect} from 'react';
14+
import {
15+
Animated,
16+
PanResponder,
17+
StyleSheet,
18+
useAnimatedValue,
19+
} from 'react-native';
1520

1621
const AnExTilt = (): React.Node => {
17-
const panX = useRef(new Animated.Value(0)).current;
18-
const opacity = useRef(new Animated.Value(1)).current;
19-
const burns = useRef(new Animated.Value(1.15)).current;
22+
const panX = useAnimatedValue(0);
23+
const opacity = useAnimatedValue(1);
24+
const burns = useAnimatedValue(1.15);
2025

2126
const tiltPanResponder = PanResponder.create({
2227
onStartShouldSetPanResponder: () => true,

packages/rn-tester/js/examples/Experimental/Compatibility/CompatibilityAnimatedPointerMove.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import type {RNTesterModuleExample} from '../../../types/RNTesterTypes';
1212

1313
import ToggleNativeDriver from '../../Animated/utils/ToggleNativeDriver';
1414
import * as React from 'react';
15-
import {useRef, useState} from 'react';
16-
import {Animated, StyleSheet, Text} from 'react-native';
15+
import {useState} from 'react';
16+
import {Animated, StyleSheet, Text, useAnimatedValue} from 'react-native';
1717

1818
const WIDTH = 200;
1919
const HEIGHT = 250;
@@ -39,8 +39,8 @@ const styles = StyleSheet.create({
3939
});
4040

4141
function CompatibilityAnimatedPointerMove(): React.Node {
42-
const xCoord = useRef(new Animated.Value(0)).current;
43-
const yCoord = useRef(new Animated.Value(0)).current;
42+
const xCoord = useAnimatedValue(0);
43+
const yCoord = useAnimatedValue(0);
4444
const [useNativeDriver, setUseNativeDriver] = useState(true);
4545

4646
return (

packages/rn-tester/js/examples/Filter/FilterExample.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ import type {ViewStyleProp} from 'react-native/Libraries/StyleSheet/StyleSheet';
1515

1616
import React from 'react';
1717
import {useState} from 'react';
18-
import {Animated, Button, Image, StyleSheet, Text, View} from 'react-native';
18+
import {
19+
Animated,
20+
Button,
21+
Image,
22+
StyleSheet,
23+
Text,
24+
View,
25+
useAnimatedValue,
26+
} from 'react-native';
1927

2028
const alphaHotdog = require('../../assets/alpha-hotdog.png');
2129
const hotdog = require('../../assets/hotdog.jpg');
@@ -278,7 +286,7 @@ exports.examples = [
278286
] as Array<RNTesterModuleExample>;
279287

280288
const AnimatedBlurExample = () => {
281-
const animatedValue = React.useRef(new Animated.Value(0)).current;
289+
const animatedValue = useAnimatedValue(0);
282290
const [isBlurred, setIsBlurred] = React.useState(false);
283291

284292
const onPress = () => {

packages/rn-tester/js/examples/Transform/TransformExample.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
1212
import type {AnimatedNode} from 'react-native/Libraries/Animated/AnimatedExports';
1313

1414
import * as React from 'react';
15-
import {useEffect, useRef, useState} from 'react';
16-
import {Animated, Easing, StyleSheet, Text, View} from 'react-native';
15+
import {useEffect, useState} from 'react';
16+
import {
17+
Animated,
18+
Easing,
19+
StyleSheet,
20+
Text,
21+
View,
22+
useAnimatedValue,
23+
} from 'react-native';
1724

1825
function AnimateTransformSingleProp() {
1926
const [theta] = useState(new Animated.Value(45));
@@ -53,7 +60,7 @@ function AnimateTransformSingleProp() {
5360
}
5461

5562
function TransformOriginExample() {
56-
const rotateAnim = useRef(new Animated.Value(0)).current;
63+
const rotateAnim = useAnimatedValue(0);
5764

5865
useEffect(() => {
5966
Animated.loop(

0 commit comments

Comments
 (0)