从 URL 加载 Lottie 动画
要从已放入 public/
文件夹的 URL 加载 Lottie 动画,请使用 fetch
和 Remotion 的 delayRender()
函数。
资源必须支持 CORS。
使用 LottieAnimationData
类型来使用 React 的 useState()
保持状态,并且仅在数据被获取后渲染 <Lottie>
组件。
Animation.tsxtsx
import {Lottie ,LottieAnimationData } from "@remotion/lottie";import {useEffect ,useState } from "react";import {cancelRender ,continueRender ,delayRender } from "remotion";constBalloons = () => {const [handle ] =useState (() =>delayRender ("Loading Lottie animation"));const [animationData ,setAnimationData ] =useState <LottieAnimationData | null>(null);useEffect (() => {fetch ("https://assets4.lottiefiles.com/packages/lf20_zyquagfl.json").then ((data ) =>data .json ()).then ((json ) => {setAnimationData (json );continueRender (handle );}).catch ((err ) => {cancelRender (err );});}, [handle ]);if (!animationData ) {return null;}return <Lottie animationData ={animationData } />;};
Animation.tsxtsx
import {Lottie ,LottieAnimationData } from "@remotion/lottie";import {useEffect ,useState } from "react";import {cancelRender ,continueRender ,delayRender } from "remotion";constBalloons = () => {const [handle ] =useState (() =>delayRender ("Loading Lottie animation"));const [animationData ,setAnimationData ] =useState <LottieAnimationData | null>(null);useEffect (() => {fetch ("https://assets4.lottiefiles.com/packages/lf20_zyquagfl.json").then ((data ) =>data .json ()).then ((json ) => {setAnimationData (json );continueRender (handle );}).catch ((err ) => {cancelRender (err );});}, [handle ]);if (!animationData ) {return null;}return <Lottie animationData ={animationData } />;};