javascript - 如何在 componentWillReceiveProps 上运行 Lottie 动画?

标签 javascript react-native jsx expo lottie

我想在我的组件收到更新的 Prop 时运行动画。

示例代码:

import React from 'react';
import Animation from 'lottie-react-native';

export default class BasicExample extends React.Component {
  componentDidMount() {
// This works
if(this.props.displayAnimation)
    this.animation.play();
  }

  componentWillReceiveProps(nextProps) {
      console.log("Component will receive new prop")
      if(nextProps.displayAnimation){
         this.animation.play();
      }
   }

  render() {
    return (
      <Animation
        ref={animation => { this.animation = animation; }}
        style={{
          width: 200,
          height: 200,
        }}
        source={require('../path/to/animation.json')}
      />
    );
  }
}

因此 componentWillReceiveProps 上的 this.animation.play(); 不起作用。根据我阅读的几个文档,我意识到这可能不是正确的方法,因为 componentWillReceiveProps 上的 thiscomponentDidMount< 上的 this 不同

我尝试传递一个状态来强制更新组件,但 React 不会更新动画组件。

关于如何在 componentWillReceiveProps 上播放动画的建议

最佳答案

componentDidUpdate 中尝试一下。

关于javascript - 如何在 componentWillReceiveProps 上运行 Lottie 动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45771458/

相关文章:

node.js - Promise 对象在 React Native 项目中不起作用

ios - 等效于RCTViewManager中的viewDidAppear

reactjs - React 组件中的 JSX 将西里尔文文本显示为 habra-codabra

javascript - 单击按钮时绘制多行 JavaScript - 不使用 Canvas

android - React-Native - 图片未在 Android 模拟器中显示

vim - 如何更改 VIM 中的 JSX 缩进规则?

javascript - 如何在 React/jsx 中以三元组的形式显示真实情况下的组件和变量?

javascript - 当我只想显示时间时,脚本打印未定义

javascript - 我如何在JS中将数字(例如: 2. 12)四舍五入到最接近的十分之一(2.1)

javascript - 将 Angular 与 Twitter Bootstrap 模板结合使用