重新启动 React Native 动画当组件的属性发生变化时

huangapple go评论45阅读模式
英文:

Restart React Native Animation When Component's Props Change

问题

我有一个React Native组件,用于渲染通知文本。我希望文本在稍后淡入和淡出。当设置新的通知文本时,我希望重新启动相同的动画。

我有以下代码:

export default function Notification({ notification }) {
    if (!notification) {
        return null;
    }

    const [fadeAnimation] = useState(new Animated.Value(0));

    useEffect(() => {
        Animated.sequence([
            Animated.timing(fadeAnimation, {
                toValue: 1,
                duration: 500,
                useNativeDriver: true,
            }),
            Animated.delay(3000),
            Animated.timing(fadeAnimation, {
                toValue: 0,
                duration: 500,
                useNativeDriver: true,
            })]).start()
    }, []);

    return (
        <Animated.View style={{ opacity: fadeAnimation, }}>
            <View style={styles.notificaton}>
                <Text style={styles.text}>{notification}</Text>
            </View>
        </Animated.View>
    )
}

我了解到我应该能够使用 setValue(0) 来重置动画,然而,我不知道在哪里和何时调用它。

英文:

I have a React Native component rendering a notification text. I want the text to fade in and fade out after a small delay. When a new notification text is set, I want to restart the same animation.

I have the following code:

export default function Notification({ notification }) {
    if (!notification) {
        return null;
    }

    const [fadeAnimation] = useState(new Animated.Value(0));

    useEffect(() =&gt; {
        Animated.sequence([
            Animated.timing(fadeAnimation, {
                toValue: 1,
                duration: 500,
                useNativeDriver: true,
            }),
            Animated.delay(3000),
            Animated.timing(fadeAnimation, {
                toValue: 0,
                duration: 500,
                useNativeDriver: true,
            })]).start()
    }, []);

    return (
        &lt;Animated.View style={{ opacity: fadeAnimation, }}&gt;
            &lt;View style={styles.notificaton}&gt;
                &lt;Text style={styles.text}&gt;{notification}&lt;/Text&gt;
            &lt;/View&gt;
        &lt;/Animated.View &gt;
    )
}

I read that I should be able to reset the animation with setValue(0) again, however I do not know where and when to call it.

答案1

得分: 1

work for me

import React, { Component } from 'react';
import { Animated, View } from 'react-native';

class Testing extends Component {
  constructor(props) {
    super(props);
    this.animatedValue = new Animated.Value(0);
  }

  componentDidUpdate(prevProps) {
    if (prevProps.myProp !== this.props.myProp) {
      this.startAnimation();
    }
  }

  startAnimation() {
    this.animatedValue.setValue(0);
    Animated.timing(this.animatedValue, {
      toValue: 1,
      duration: 1000,
      useNativeDriver: true,
    }).start();
  }

  render() {
    const { myProp } = this.props;
    const opacity = this.animatedValue.interpolate({
      inputRange: [0, 1],
      outputRange: [0, 1],
    });

    return (
      <View>
        <Animated.Text style={{ opacity }}>
          My prop val : {myProp}
        </Animated.Text>
      </View>
    );
  }
}

export default Testing;
英文:

work for me

import React, { Component } from &#39;react&#39;;
import { Animated, View } from &#39;react-native&#39;;

class Testing extends Component {
  constructor(props) {
    super(props);
    this.animatedValue = new Animated.Value(0);
  }

  componentDidUpdate(prevProps) {
    if (prevProps.myProp !== this.props.myProp) {
      this.startAnimation();
    }
  }

  startAnimation() {
    this.animatedValue.setValue(0);
    Animated.timing(this.animatedValue, {
      toValue: 1,
      duration: 1000,
      useNativeDriver: true,
    }).start();
  }

  render() {
    const { myProp } = this.props;
    const opacity = this.animatedValue.interpolate({
      inputRange: [0, 1],
      outputRange: [0, 1],
    });

    return (
      &lt;View&gt;
        &lt;Animated.Text style={{ opacity }}&gt;
          My prop val : {myProp}
        &lt;/Animated.Text&gt;
      &lt;/View&gt;
    );
  }
}

export default Testing;

huangapple
  • 本文由 发表于 2023年2月19日 05:12:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定