🎥 Make videos programmatically with React
improve docs
Jonny Burger81a15399cac6
diff --git a/packages/core/src/ready-manager.ts b/packages/core/src/ready-manager.tsindex 982905975d..9005c1dba0 100644--- a/packages/core/src/ready-manager.ts+++ b/packages/core/src/ready-manager.ts@@ -9,6 +9,12 @@ if (typeof window !== 'undefined') {let handles: number[] = [];const timeouts: {[key: string]: number | NodeJS.Timeout} = {};+/**+ * Call this function to tell Remotion to wait before capturing this frame until data has loaded. Use continueRender() to unblock the render.+ * @param description _optional_ A description to identify the call in case it does time out.+ * @returns {number} An identifier to be passed to continueRender().+ * @link https://www.remotion.dev/docs/delay-render+ */export const delayRender = (description?: string): number => {if (typeof description !== 'string' && typeof description !== 'undefined') {throw new Error(@@ -46,6 +52,11 @@ export const delayRender = (description?: string): number => {return handle;};+/**+ * Unblock a render that has been blocked by delayRender()+ * @param handle The return value of delayRender().+ * @link https://www.remotion.dev/docs/continue-render+ */export const continueRender = (handle: number): void => {if (typeof handle === 'undefined') {throw new TypeError(diff --git a/packages/docs/docs/delay-render.md b/packages/docs/docs/delay-render.mdindex 296dc4badb..bddc50c32f 100644--- a/packages/docs/docs/delay-render.md+++ b/packages/docs/docs/delay-render.md@@ -4,43 +4,34 @@ title: delayRender() and continueRender()sidebar_label: delayRender()----By calling `delayRender`, you are signaling that a frame should not be immediately rendered and instead should wait on an asynchronous task to complete.+By calling `delayRender()`, you are signaling that a frame should not be immediately rendered and instead should wait on an asynchronous task to complete.This method is useful if you for example want to call an API to fetch data before you render.-`delayRender` returns an identifier. Once you have fetched data or finished the asynchronous task, you should call `continueRender(identifier)` to let Remotion know that you are now ready to render.--## Useful to know--- You need to call `continueRender()` within 30 seconds of page load. This is the default timeout of puppeteer and it will throw if you miss to call `continueRender()`.--- For every frame that is rendered, the whole page is reloaded, so the whole lifecycle of `delayRender` and `continueRender` happens on every frame. If you are doing an API request, you can speed up the render and avoid rate limits by caching the request, for example by storing the data in `localStorage`.--- You can call `delayRender` multiple times. The render will be blocked for as long as at least one blocking handle exists and that has not been cleared by `continueRender()`.--- You should put `delayRender()` calls inside your components rather than placing them as a top-level statement, to avoid blocking a render if a different composition is rendered. Also, in the example below the call is wrapped in a `useState()` to avoid creating multiple blocking calls when the component rerenders.+`delayRender()` returns an identifier. Once you have fetched data or finished the asynchronous task, you should call `continueRender(identifier)` to let Remotion know that you are now ready to render.## Example-```tsx twoslash-import { useEffect, useState } from "react";+```tsx twoslash {6, 13}+import { useCallback, useEffect, useState } from "react";import { continueRender, delayRender } from "remotion";export const MyVideo = () => {const [data, setData] = useState(null);const [handle] = useState(() => delayRender());- const fetchData = async () => {+ const fetchData = useCallback(async () => {const response = await fetch("http://example.com/api");const json = await response.json();setData(json);continueRender(handle);- };+ }, []);useEffect(() => {fetchData();}, []);+return (<div>{data ? (@@ -51,6 +42,16 @@ export const MyVideo = () => {};```+## Useful to know++- You need to call `continueRender()` within 30 seconds of page load. This is the default timeout of puppeteer and it will throw if you miss to call `continueRender()`.++- For every frame that is rendered, the whole page is reloaded, so the whole lifecycle of `delayRender` and `continueRender` happens on every frame. If you are doing an API request, you can speed up the render and avoid rate limits by caching the request, for example by storing the data in `localStorage`.++- You can call `delayRender` multiple times. The render will be blocked for as long as at least one blocking handle exists and that has not been cleared by `continueRender()`.++- You should put `delayRender()` calls inside your components rather than placing them as a top-level statement, to avoid blocking a render if a different composition is rendered. Also, in the example below the call is wrapped in a `useState()` to avoid creating multiple blocking calls when the component rerenders.+## See also- [Data fetching](/docs/data-fetching)