Skip to content

🎥 Make videos programmatically with React

Update use-video-texture.ts

Jonny Burger6d27bfa420be
diff --git a/packages/three/src/use-video-texture.ts b/packages/three/src/use-video-texture.ts
index 794edd9e98..414d7311d3 100644
--- a/packages/three/src/use-video-texture.ts
+++ b/packages/three/src/use-video-texture.ts
@@ -32,23 +32,38 @@ export const useVideoTexture = (
const [vidText] = useState(
() => import('three/src/textures/VideoTexture.js')
);
+ const [error, setError] = useState<Error | null>(null);
const frame = useCurrentFrame();
+ if (error) {
+ throw error;
+ }
+
const onReady = useCallback(() => {
- vidText.then(({VideoTexture}) => {
- if (!videoRef.current) {
- throw new Error('Video not ready');
- }
-
- const vt = new VideoTexture(videoRef.current);
- videoRef.current.width = videoRef.current.videoWidth;
- videoRef.current.height = videoRef.current.videoHeight;
- setVideoTexture(vt);
- continueRender(loaded);
- });
+ vidText
+ .then(({VideoTexture}) => {
+ if (!videoRef.current) {
+ throw new Error('Video not ready');
+ }
+
+ const vt = new VideoTexture(videoRef.current);
+ videoRef.current.width = videoRef.current.videoWidth;
+ videoRef.current.height = videoRef.current.videoHeight;
+ setVideoTexture(vt);
+ continueRender(loaded);
+ })
+ .catch((err) => {
+ setError(err);
+ });
}, [loaded, vidText, videoRef]);
React.useEffect(() => {
+ if (typeof document === 'undefined') {
+ // Do not trigger onReady in SSR
+ continueRender(loaded);
+ return;
+ }
+
if (!videoRef.current) {
return;
}