Skip to content

Comments

add animationRef prop#11

Merged
lazysergey merged 1 commit intocrello:masterfrom
good-idea:master
Mar 2, 2020
Merged

add animationRef prop#11
lazysergey merged 1 commit intocrello:masterfrom
good-idea:master

Conversation

@good-idea
Copy link

@good-idea good-idea commented Feb 19, 2020

This PR adds the ability to use the animationItem returned from lottie-web in parent components. This allows for more control over the animation with all of the built-in functions like goToAndStop, goToAndPlay, etc.

In my use case, I want to have an animation play frames 0-100, then loop from 50-100:

const MyAnimation = () => {
  const animationRef = useRef(null)
  const onLoopComplete = () => {
    if (animationRef && animationRef.current) {
      animationRef.current.goToAndPlay(50, true)
    }
  }

  const lottieEventListeners = [
    {
      callback: onLoopComplete,
      name: 'loopComplete',
    },
  ]

  return (
     <Lottie
      config={config}
      lottieEventListeners={lottieEventListeners}
      animationRef={animationRef}
    />
  )
}

@lazysergey
Copy link
Collaborator

@good-idea hi mate thanks for PR, will review/merge it asap

@lazysergey lazysergey merged commit cad8d8b into crello:master Mar 2, 2020
@lazysergey
Copy link
Collaborator

@good-idea I have question - React.MutableRef-> shouldn't it be React.MutableRefObject instead?

@good-idea
Copy link
Author

@lazysergey yep, you're right! I just opened a new PR

@flo-sch
Copy link

flo-sch commented May 26, 2020

Hi guys, sorry to comment here, but how does this work with TypeScript?

I have tried:

import { AnimationItem } from 'lottie-web';

const Animation = () => {
  const animationRef = useRef<AnimationItem>(null);

  return (
    <Lottie
      // ...
      animationRef={animationRef}
    />
  );
};

Type 'RefObject<AnimationItem>' is not assignable to type 'MutableRefObject<AnimationItem>'.

And without initializing to null, but I really doubt that ref is supposed to be actually muted anyway?

import { AnimationItem } from 'lottie-web';

const Animation = () => {
  const animationRef = useRef<AnimationItem>();

  return (
    <Lottie
      // ...
      animationRef={animationRef}
    />
  );
};

Type 'MutableRefObject<AnimationItem | undefined>' is not assignable to type 'MutableRefObject<AnimationItem>'.
Type 'AnimationItem | undefined' is not assignable to type 'AnimationItem>'.
Type 'undefined' is not assignable to type 'AnimationItem>'.

@flo-sch
Copy link

flo-sch commented May 26, 2020

EDIT: I managed to workaround by asserting a MutableRefObject:

const animation = useRef<AnimationItem>(null) as React.MutableRefObject<AnimationItem>;

But shouldn't it be just a RefObject in the end?

@good-idea
Copy link
Author

@flo-sch I've updated this in #19, thanks for checking in here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants