Learning React here, and I'm simply trying to display a lottie animation. Seems fairly straight-forward, but I'm having some trouble with the useEffect hook. I'm using the hook to render the animation on page load, but it keeps repeating itself (sometimes twice or more). I know this is because useEffect will fire more than once, but I'm not sure how to handle this. I just want one animation on the page. How can I achieve this?
import React, { useEffect, useRef } from "react";
import "./index.scss";
import lottie from "lottie-web";
const Hero = () => {
const lottieContainer = useRef(null);
useEffect(() => {
lottie.loadAnimation({
container: lottieContainer.current,
renderer: "svg",
loop: true,
autoplay: true,
animationData: require("../../assets/Lottie/developer.json"),
});
}, []);
return (
<div className="hero">
<div ref={lottieContainer}></div>
</div>
);
};
export default Hero;