Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add animationRef prop #11

Merged
merged 1 commit into from
Mar 2, 2020
Merged

add animationRef prop #11

merged 1 commit into from
Mar 2, 2020

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.

None yet

3 participants