@@ -20,12 +20,12 @@ For example, a container view that fades in when it is mounted may look like thi
20
20
<Tabs groupId =" language " queryString defaultValue ={constants.defaultSnackLanguage} values ={constants.snackLanguages} >
21
21
<TabItem value =" javascript " >
22
22
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';
26
26
27
27
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
29
29
30
30
useEffect(() => {
31
31
Animated.timing(fadeAnim, {
@@ -74,15 +74,13 @@ export default () => {
74
74
<TabItem value =" typescript " >
75
75
76
76
``` 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';
81
79
82
80
type FadeInViewProps = PropsWithChildren<{style: ViewStyle}>;
83
81
84
82
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
86
84
87
85
useEffect(() => {
88
86
Animated.timing(fadeAnim, {
@@ -594,8 +592,8 @@ Note that in order to get this to work on **Android** you need to set the follow
594
592
UIManager .setLayoutAnimationEnabledExperimental (true );
595
593
```
596
594
597
- ``` SnackPlayer name=LayoutAnimations&supportedPlatforms=ios,android
598
- import React from 'react';
595
+ ``` SnackPlayer name=LayoutAnimations
596
+ import React, {useState} from 'react';
599
597
import {
600
598
NativeModules,
601
599
LayoutAnimation,
@@ -610,32 +608,30 @@ const {UIManager} = NativeModules;
610
608
UIManager.setLayoutAnimationEnabledExperimental &&
611
609
UIManager.setLayoutAnimationEnabledExperimental(true);
612
610
613
- export default class App extends React.Component {
614
- state = {
611
+ export default function App() {
612
+ const [ state, setState] = useState( {
615
613
w: 100,
616
614
h: 100,
617
- };
615
+ }) ;
618
616
619
- _onPress = () => {
617
+ const onPress = () => {
620
618
// Animate the update
621
619
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});
623
621
};
624
622
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
+ );
639
635
}
640
636
641
637
const styles = StyleSheet.create({
0 commit comments