Skip to content

Commit d596f06

Browse files
authored
fix Animation examples, allow web preview (#4712)
1 parent f7600c5 commit d596f06

File tree

4 files changed

+37
-45
lines changed

4 files changed

+37
-45
lines changed

docs/animated.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,20 @@ The core workflow for creating an animation is to create an `Animated.Value`, ho
1313

1414
The following example contains a `View` which will fade in and fade out based on the animated value `fadeAnim`
1515

16-
```SnackPlayer name=Animated%20Example&supportedPlatforms=ios,android
17-
import React from 'react';
16+
```SnackPlayer name=Animated%20Example
17+
import React, {useRef} from 'react';
1818
import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context';
1919
import {
2020
Animated,
2121
Text,
2222
View,
2323
StyleSheet,
2424
Button,
25-
useAnimatedValue,
2625
} from 'react-native';
2726
2827
const App = () => {
2928
// fadeAnim will be used as the value for opacity. Initial Value: 0
30-
const fadeAnim = useAnimatedValue(0);
29+
const fadeAnim = useRef(new Animated.Value(0)).current;
3130
3231
const fadeIn = () => {
3332
// Will change fadeAnim value to 1 in 5 seconds

docs/animations.md

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ For example, a container view that fades in when it is mounted may look like thi
2020
<Tabs groupId="language" queryString defaultValue={constants.defaultSnackLanguage} values={constants.snackLanguages}>
2121
<TabItem value="javascript">
2222

23-
```SnackPlayer ext=js&supportedPlatforms=ios,android
24-
import React, {useEffect} from 'react';
25-
import {Animated, Text, View, useAnimatedValue} from 'react-native';
23+
```SnackPlayer ext=js
24+
import React, {useEffect, useRef} from 'react';
25+
import {Animated, Text, View} from 'react-native';
2626
2727
const FadeInView = props => {
28-
const fadeAnim = useAnimatedValue(0); // Initial value for opacity: 0
28+
const fadeAnim = useRef(new Animated.Value(0)).current; // Initial value for opacity: 0
2929
3030
useEffect(() => {
3131
Animated.timing(fadeAnim, {
@@ -74,15 +74,13 @@ export default () => {
7474
<TabItem value="typescript">
7575

7676
```SnackPlayer ext=tsx
77-
import React, {useEffect} from 'react';
78-
import {Animated, Text, View, useAnimatedValue} from 'react-native';
79-
import type {PropsWithChildren} from 'react';
80-
import type {ViewStyle} from 'react-native';
77+
import React, {useEffect, useRef, type PropsWithChildren} from 'react';
78+
import {Animated, Text, View, type ViewStyle} from 'react-native';
8179
8280
type FadeInViewProps = PropsWithChildren<{style: ViewStyle}>;
8381
8482
const FadeInView: React.FC<FadeInViewProps> = props => {
85-
const fadeAnim = useAnimatedValue(0); // Initial value for opacity: 0
83+
const fadeAnim = useRef(new Animated.Value(0)).current; // Initial value for opacity: 0
8684
8785
useEffect(() => {
8886
Animated.timing(fadeAnim, {
@@ -594,8 +592,8 @@ Note that in order to get this to work on **Android** you need to set the follow
594592
UIManager.setLayoutAnimationEnabledExperimental(true);
595593
```
596594

597-
```SnackPlayer name=LayoutAnimations&supportedPlatforms=ios,android
598-
import React from 'react';
595+
```SnackPlayer name=LayoutAnimations
596+
import React, {useState} from 'react';
599597
import {
600598
NativeModules,
601599
LayoutAnimation,
@@ -610,32 +608,30 @@ const {UIManager} = NativeModules;
610608
UIManager.setLayoutAnimationEnabledExperimental &&
611609
UIManager.setLayoutAnimationEnabledExperimental(true);
612610
613-
export default class App extends React.Component {
614-
state = {
611+
export default function App() {
612+
const [state, setState] = useState({
615613
w: 100,
616614
h: 100,
617-
};
615+
});
618616
619-
_onPress = () => {
617+
const onPress = () => {
620618
// Animate the update
621619
LayoutAnimation.spring();
622-
this.setState({w: this.state.w + 15, h: this.state.h + 15});
620+
setState({w: state.w + 15, h: state.h + 15});
623621
};
624622
625-
render() {
626-
return (
627-
<View style={styles.container}>
628-
<View
629-
style={[styles.box, {width: this.state.w, height: this.state.h}]}
630-
/>
631-
<TouchableOpacity onPress={this._onPress}>
632-
<View style={styles.button}>
633-
<Text style={styles.buttonText}>Press me!</Text>
634-
</View>
635-
</TouchableOpacity>
636-
</View>
637-
);
638-
}
623+
return (
624+
<View style={styles.container}>
625+
<View
626+
style={[styles.box, {width: state.w, height: state.h}]}
627+
/>
628+
<TouchableOpacity onPress={onPress}>
629+
<View style={styles.button}>
630+
<Text style={styles.buttonText}>Press me!</Text>
631+
</View>
632+
</TouchableOpacity>
633+
</View>
634+
);
639635
}
640636
641637
const styles = StyleSheet.create({

docs/easing.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ The following helpers are used to modify other easing functions.
4848
<Tabs groupId="language" queryString defaultValue={constants.defaultSnackLanguage} values={constants.snackLanguages}>
4949
<TabItem value="javascript">
5050

51-
```SnackPlayer name=Easing%20Demo&ext=js&supportedPlatforms=ios,android
52-
import React from 'react';
51+
```SnackPlayer name=Easing%20Demo&ext=js
52+
import React, {useRef} from 'react';
5353
import {
5454
Animated,
5555
Easing,
@@ -59,12 +59,11 @@ import {
5959
Text,
6060
TouchableOpacity,
6161
View,
62-
useAnimatedValue,
6362
} from 'react-native';
6463
import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context';
6564
6665
const App = () => {
67-
const opacity = useAnimatedValue(0);
66+
const opacity = useRef(new Animated.Value(0)).current;
6867
6968
const animate = easing => {
7069
opacity.setValue(0);
@@ -210,7 +209,7 @@ export default App;
210209
<TabItem value="typescript">
211210

212211
```SnackPlayer name=Easing%20Demo&ext=tsx
213-
import React from 'react';
212+
import React, {useRef} from 'react';
214213
import {
215214
Animated,
216215
Easing,
@@ -220,13 +219,12 @@ import {
220219
Text,
221220
TouchableOpacity,
222221
View,
223-
useAnimatedValue,
224222
type EasingFunction,
225223
} from 'react-native';
226224
import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context';
227225
228226
const App = () => {
229-
const opacity = useAnimatedValue(0);
227+
const opacity = useRef(new Animated.Value(0)).current;
230228
231229
const animate = (easing: EasingFunction) => {
232230
opacity.setValue(0);

docs/transforms.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,19 +211,18 @@ The `transformOrigin` property sets the origin for a view's transformations. The
211211

212212
# Example
213213

214-
```SnackPlayer name=TransformOrigin%20Example&supportedPlatforms=ios,android
215-
import React, {useEffect} from 'react';
214+
```SnackPlayer name=TransformOrigin%20Example
215+
import React, {useEffect, useRef} from 'react';
216216
import {
217217
Animated,
218218
View,
219219
StyleSheet,
220220
Easing,
221-
useAnimatedValue,
222221
} from 'react-native';
223222
import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context';
224223
225224
const App = () => {
226-
const rotateAnim = useAnimatedValue(0);
225+
const rotateAnim = useRef(new Animated.Value(0)).current;
227226
228227
useEffect(() => {
229228
Animated.loop(

0 commit comments

Comments
 (0)