@@ -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, {
@@ -594,8 +592,8 @@ Note that in order to get this to work on **Android** you need to set the follow
594592UIManager .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';
599597import {
600598 NativeModules,
601599 LayoutAnimation,
@@ -610,32 +608,30 @@ const {UIManager} = NativeModules;
610608UIManager.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
641637const styles = StyleSheet.create({
0 commit comments