React Native Lottie:无法读取未定义的属性“播放”

Jon*_*nas 0 react-native react-native-sound

我正在使用 Lottie 命令式 API 来显示循环动画。除了使用React Native Sound 的组件外,命令式 API 在我的所有组件上都运行良好。我认为问题是两个库都是用.play(). 这可能吗?

洛蒂:this.animation.play(); 反应原生声音:this.sound.play()

调用 Lottie 方法后,我收到错误消息:

无法读取未定义的属性“播放”

有任何想法吗?

提前致谢。

Jon*_*nas 5

我终于想通了。React Native Sound 当然不是问题——它可能只是延迟了 Lottie 的初始化,因此animation在我调用它时仍然未定义。

解决方案是内置一个计时器,并已在此线程中提出建议:https : //github.com/airbnb/lottie-react-native/issues/21

  componentDidMount() {
      this.initAnimation();
  }

  initAnimation(){
    if (!this.animation){
      setTimeout(() => {
        this.initAnimation();
      }, 100);
    } else {
        this.animation.play();
    }
  }
Run Code Online (Sandbox Code Playgroud)