0

I would like to run the animation when my component receives an updated prop.

Sample code:

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')}
      />
    );
  }
}

So the this.animation.play(); on the componentWillReceiveProps doesnot works. Based on several documents i read realised this is probably not the right way since the this on componentWillReceiveProps is different from this on componentDidMount

I tried passing a state to force update the component but React wont update the Animation component.

Suggestion on how to make the animation play on componentWillReceiveProps

KD.
  • 2,015
  • 3
  • 28
  • 59

1 Answers1

2

Try that in componentDidUpdateinstead.

phoa
  • 251
  • 1
  • 6