question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use one animation with multiple element

See original GitHub issue

My problem: I’m using lottie-web with reactjs. But i want use one animation with multiple element. Example: Like of posts, animation of like depends on the State being passed. It didn’t work as expected

My code: App.js:

const [isLiked, setLike] = useState(true);
  const [isLiked2, setLike2] = useState(false);
  return (
    <div className="App">
      <h1>Demo Animation</h1>
      <Like isLiked={isLiked} />
      <button
        onClick={() => setLike(!isLiked)}
        style={{ color: isLiked ? "green" : null }}
      >
        Like
      </button>
      <Like isLiked={isLiked2} />
      <button
        onClick={() => setLike2(!isLiked2)}
        style={{ color: isLiked2 ? "green" : null }}
      >
        Like2
      </button>
    </div>
  );

Like.js:

let likeAnimObj = null;

export default function Like({ isLiked }) {
  const container = useRef(null);
  const loadAnimation = () => {
    likeAnimObj = lottie.loadAnimation({
      container: container.current, // the dom element that will contain the animation
      renderer: "svg",
      loop: false,
      autoplay: false,
      animationData: animationLike // the path to the animation json
    });
  };
  useEffect(() => {
    loadAnimation();
    isLiked
      ? likeAnimObj.goToAndStop(60, true)
      : likeAnimObj.goToAndStop(0, true);
  }, []);

  const firstUpdate = useRef(true);
  useEffect(() => {
    if (firstUpdate.current) {
      firstUpdate.current = false;
      return;
    }
    likeAnimObj.destroy();
    loadAnimation();
    isLiked
      ? likeAnimObj.playSegments([0, 60], true)
      : // : likeAnimObj.playSegments([60, 110], true);
        likeAnimObj.goToAndStop(0, true);
  }, [isLiked]);

  return <div style={{ width: 200, margin: "0 auto" }} ref={container} />;
}

You can run my example on codesandbox: here I hope your response. Thank you so much!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

1reaction
namlq93commented, Apr 27, 2020

@bodymovin Thank you so much. It’s very helpful with me. Actually, my output is a little different so I’ve edited it a bit. You can see here. Even though I still don’t fully understand, but it did work anyway. Thank you again ❤️

1reaction
bodymovincommented, Apr 27, 2020

I’m not that familiar with react hooks, but I think you should have two hooks, one similar to this: const [animation, setAnimation] = useState(null) and the other: useEffect(()=>{setAnimation(loadAnimation())},[]) and make sure that your loadAnimation method returns the animation instance. Here is a version that I think gets closer to what you need, but I’m not a react hooks expert and you probably need to adjust it to your needs. https://codesandbox.io/s/lottie-web-animation-gotostop-3-g5cq4

Read more comments on GitHub >

github_iconTop Results From Across the Web

Play multiple CSS animations at the same time - Stack Overflow
While it is true that you cannot play two transform animations at the same time (because one transform would overwrite the other), it...
Read more >
Apply multiple animation effects to one object - Microsoft Support
Select the object on the slide that you want to animate. · On the Animations tab, click Animation Pane. Open the Animation Pane...
Read more >
Animating Many Elements and the Animate Method | kirupa.com
Animating multiple elements using the animate method is scarily very similar to animating just a single element using the animate method. To cut ......
Read more >
Using Multi-Step Animations and Transitions | CSS-Tricks
If we set up a keyframe animation to change the background color of an element to change from orange to black (because orange...
Read more >
Applying animation to multiple objects on one page - LinkedIn
So you need to apply that preset to more than one object. Fortunately, you don't have to apply animations one by one. If...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found