Skip to content

🎥 Make videos programmatically with React

loading from a remote URL

Jonny Burger6fe93b1f3fd9
diff --git a/packages/docs/docs/lottie/lottie-remote.md b/packages/docs/docs/lottie/lottie-remote.md
new file mode 100644
index 0000000000..203c7b1b20
--- /dev/null
+++ b/packages/docs/docs/lottie/lottie-remote.md
@@ -0,0 +1,48 @@
+---
+id: lottie-remote
+sidebar_label: "Loading from a URL"
+title: "Loading Lottie animations from a URL"
+slug: remote
+---
+
+In order to load a Lottie animation from a URL that has been put into the `public/` folder, use [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) and Remotion's [`delayRender()`](/docs/delay-render) function.
+
+The resource must support [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS).
+
+Use the `LottieAnimationData` type to keep a state using React's `useState()` and only render the [`<Lottie>`](/docs/lottie/lottie) component once the data has been fetched.
+
+```tsx twoslash title="Animation.tsx"
+import { continueRender, delayRender } from "remotion";
+import { useEffect, useState } from "react";
+import { Lottie, LottieAnimationData } from "@remotion/lottie";
+
+const Balloons = () => {
+ 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) => {
+ console.log("Animation failed to load", err);
+ });
+ }, [handle]);
+
+ if (!animationData) {
+ return null;
+ }
+
+ return <Lottie animationData={animationData} />;
+};
+```
+
+## See also
+
+- [`<Lottie>`](/docs/lottie/lottie)
+- [Loading from `staticFile()`](/docs/staticfile)
diff --git a/packages/docs/docs/lottie/lottie-staticfile.md b/packages/docs/docs/lottie/lottie-staticfile.md
index c0b8cabf4a..4cdf673920 100644
--- a/packages/docs/docs/lottie/lottie-staticfile.md
+++ b/packages/docs/docs/lottie/lottie-staticfile.md
@@ -43,3 +43,4 @@ const Balloons = () => {
## See also
- [`<Lottie>`](/docs/lottie/lottie)
+- [Loading from a URL](/docs/remote)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index ff6b706a60..f47ca193c2 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -234,7 +234,11 @@ module.exports = {
type: "doc",
id: "lottie/lottie-index",
},
- items: ["lottie/lottie-comp", "lottie/lottie-staticfile"],
+ items: [
+ "lottie/lottie-comp",
+ "lottie/lottie-staticfile",
+ "lottie/lottie-remote",
+ ],
},
{
type: "category",