@@ -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
2727const 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
8280type FadeInViewProps = PropsWithChildren<{style: ViewStyle}>;
8381
8482const 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, {
@@ -592,7 +590,7 @@ UIManager.setLayoutAnimationEnabledExperimental(true);
592590```
593591
594592``` SnackPlayer name=LayoutAnimations&supportedPlatforms=ios,android
595- import React from 'react';
593+ import React, {useState} from 'react';
596594import {
597595 NativeModules,
598596 LayoutAnimation,
@@ -607,32 +605,30 @@ const {UIManager} = NativeModules;
607605UIManager.setLayoutAnimationEnabledExperimental &&
608606 UIManager.setLayoutAnimationEnabledExperimental(true);
609607
610- export default class App extends React.Component {
611- state = {
608+ export default function App() {
609+ const [ state, setState] = useState( {
612610 w: 100,
613611 h: 100,
614- };
612+ }) ;
615613
616- _onPress = () => {
614+ const onPress = () => {
617615 // Animate the update
618616 LayoutAnimation.spring();
619- this. setState({w: this. state.w + 15, h: this. state.h + 15});
617+ setState({w: state.w + 15, h: state.h + 15});
620618 };
621619
622- render() {
623- return (
624- <View style={styles.container}>
625- <View
626- style={[styles.box, {width: this.state.w, height: this.state.h}]}
627- />
628- <TouchableOpacity onPress={this._onPress}>
629- <View style={styles.button}>
630- <Text style={styles.buttonText}>Press me!</Text>
631- </View>
632- </TouchableOpacity>
633- </View>
634- );
635- }
620+ return (
621+ <View style={styles.container}>
622+ <View
623+ style={[styles.box, {width: state.w, height: state.h}]}
624+ />
625+ <TouchableOpacity onPress={onPress}>
626+ <View style={styles.button}>
627+ <Text style={styles.buttonText}>Press me!</Text>
628+ </View>
629+ </TouchableOpacity>
630+ </View>
631+ );
636632}
637633
638634const styles = StyleSheet.create({
0 commit comments