0

現在、私のReact Nativeアニメーションは、一度しか起こりません。私はそのコンポーネントのための私の小道具の1つが変わるたびにそれが必要になる。新しい小道具のデータが入ったときにデータ表示が変化しますが、初めてアニメートするだけです。小道具が変更/コンポーネントの更新があるたびにアニメーションが再び発生する方法はありますか?ここでReactを作成する方法ネイティブアニメーションが再び発生しますか?

私がこれまで持っているものです。

import React from 'react'; 
 
import {Animated, Easing, StyleSheet, Text, View} from 'react-native'; 
 

 

 

 

 
//Animation 
 
class FadeInView extends React.Component { 
 
    state = { 
 
    yAnimation: new Animated.Value(21), 
 
    } 
 

 
    componentDidMount() { 
 
    Animated.timing(
 
     this.state.yAnimation, 
 
     { 
 
     //easing: Easing.bounce, 
 
     toValue: 0, 
 
     duration: 150, 
 
     } 
 
    ).start(); 
 
    } 
 

 
    render() { 
 
    let { yAnimation } = this.state; 
 

 
    return (
 
     <Animated.View 
 
     style={{ 
 
      ...this.props.style, 
 
      transform: [{translateY: this.state.yAnimation}], 
 
     }} 
 
     > 
 
     {this.props.children} 
 
     </Animated.View> 
 
    ); 
 
    } 
 
} 
 

 
//Price Component 
 
export default class Price extends React.Component { 
 

 
constructor(props) { 
 
    super(props); 
 

 
    this.animateNow = false; 
 
} 
 

 
shouldComponentUpdate(nextProps, nextState) { 
 
    if (this.props.price !== nextProps.price) { 
 
    console.log('true'); 
 
    return true; 
 
    } else { 
 
    return false; 
 
    } 
 
} 
 

 
componentWillUpdate() { 
 
    if (this.props.price != this.localPrice) { 
 
    this.animateNow = true; 
 
    } 
 
} 
 

 
componentDidUpdate() { 
 
this.localPrice = this.props.price; 
 
this.animateNow = false; 
 

 
console.log(this.props.price); 
 
} 
 

 
render() { 
 
if (this.animateNow) { 
 
    return (
 
    <FadeInView> 
 
     <Text style={styles.price}>{this.props.price}</Text> 
 
    </FadeInView> 
 
); 
 
} else { 
 
    return (
 
    <View> 
 
     <Text style={styles.price}>{this.props.price}</Text> 
 
    </View> 
 
); 
 
} 
 

 
} 
 
} 
 

 
const styles = StyleSheet.create({ 
 
price: { 
 
fontFamily: 'Avenir', 
 
fontSize: 21, 
 
color: '#606060', 
 
textAlign: 'right', 
 
marginRight: 20, 
 
backgroundColor: 'transparent' 
 

 
} 
 
});

答えて

1

あなたが小道具を受信したとき、あなたはcomponentWillReceiveProps()内でそれを再度呼び出す必要があり、再びアニメーション化する場合:

playAnimation() { 
    Animated.timing(
     this.state.yAnimation, 
     { 
      toValue: 0, 
      duration: 150, 
     } 
    ).start(); 
} 

componentWillReceiveProps(next) { 
    if (next.props.changed) { 
     this.playAnimation(); 
    } 
} 
関連する問題