Why does my Lottie animation starts after content is loaded in React application?

I’m using react-lottie to animate a set of Lottie animations on a React page. I have an array of animation data (items) that each corresponds to a different animation, and I’m trying to cycle through these animations based on both user interaction and automatically.

Here’s a simplified version of the code I’m using:

Code setup:

  1. Items Array:

My items array includes the animationData which holds the json object for Lottie, title and description are strings to show on the page.

const items = [
    {
        animationData: layer1Animation,
        title: 'social gaming experiences',
        description: 'we develop entertaining engagement formats on the best social gaming platforms...',
    },
    // other items
];
  1. Default Options for Lottie:

For the react-lottie, I should have options object which holds the parameters:

const defaultOptions = {
    loop: false,
    autoplay: true,
    animationData: items[activeIndex].animationData,
    rendererSettings: {
        preserveAspectRatio: 'xMidYMid slice',
    },
};

  1. Event Listeners:

According to the answer below, I have added the eventListeners to the lottie to animate the index once the previous one is finished:

const eventListeners = [
    {
        eventName: 'complete',
        callback: handleAnimationComplete,
    },
];

const handleAnimationComplete = () => {
    setActiveIndex((prevIndex) => (prevIndex + 1) % items.length);
};

  1. Rendering:

As for the rendering, I am rendering the card_items which holds the animation and content:

<div className={styles.container}>
    <h3>but what are our actual solutions?</h3>
    <div className={styles.cart_items}>
         <div className={`${styles.cart_item} ${styles.active}`}>
             <div className={styles.image_container}>
                 <Lottie
                     key={activeIndex}
                     options={defaultOptions}
                     eventListeners={eventListeners}
                     height={400}
                 />
             </div>
             <div className={styles.content}>
                 <p>{items[activeIndex].title}</p>
                 <span>{items[activeIndex].description}</span>
             </div>
         </div>
    </div>
    <div className={styles.animation_slider}>
        {items.map((_, index) => (
            <div
                key={index}
                onClick={() => setActiveIndex(index)}
                className={index === activeIndex ? styles.active : styles.inactive}
            ></div>
        ))}
    </div>
</div>

Problem

The issue appears once the index changes. So, when the index changes and a new animation is loaded, there is a noticeable delay (approximately 0.5 seconds or more) before the new animation starts. The content updates immediately, but the Lottie animation does not, leading to a jarring visual effect where the content changes while the previous animation is still playing. This delay causes a poor user experience as it disrupts the fluidity of the animation transitions.

** I have tried:**

  • I’ve set up event listeners to trigger handleAnimationComplete which updates the index. However, the delay persists despite these listeners firing correctly.
  • I’m using key={activeIndex} on the Lottie component to ensure the component re-renders when the index changes, but this does not resolve the timing issue.

Current result

How it should be:

As showed in above result, I’m not quite sure why there is such delay in the animation. Ideally, the animation should start immediately as soon as the index changes and the content updates seamlessly, but currently there is around 0.5s delay each animation starts after the content.

Thus, to conclude, why does my lottie animation starts to autoplay with some delay?

You can also check the code at this codesandbox.

23

it could be possible that the animation is less than or more that the timeout specified, try using the onComplete event listener instead of timeout

Add this to your options

eventListeners=[
      {
        eventName: 'complete',
        callback: () => onComplete,
      },
   ]

add a new function

const onComplete () => {
    setActiveIndex((prevIndex) => (prevIndex + 1) % items.length);
}

LINK: https://github.com/chenqingspring/react-lottie?tab=readme-ov-file#props

24

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật