Skip to content

Commit 556d1be

Browse files
fix: handles view not found error gracefully (#4788)
1 parent 843c936 commit 556d1be

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

packages/react-native-video/src/core/video-view/VideoView.tsx

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import type {
88
} from '../../spec/nitro/VideoViewViewManager.nitro';
99
import type { VideoViewEvents } from '../types/Events';
1010
import type { ResizeMode } from '../types/ResizeMode';
11-
import { tryParseNativeVideoError, VideoError } from '../types/VideoError';
11+
import {
12+
tryParseNativeVideoError,
13+
VideoComponentError,
14+
VideoError,
15+
} from '../types/VideoError';
1216
import type { VideoPlayer } from '../VideoPlayer';
1317
import { NativeVideoView } from './NativeVideoView';
1418

@@ -172,7 +176,31 @@ const VideoView = React.forwardRef<VideoViewRef, VideoViewProps>(
172176
resizeMode: resizeMode,
173177
});
174178
} catch (error) {
175-
throw tryParseNativeVideoError(error);
179+
const parsedError = tryParseNativeVideoError(error);
180+
181+
if (
182+
parsedError instanceof VideoComponentError &&
183+
parsedError.code === 'view/not-found'
184+
) {
185+
// The view was not found, did view get unmounted?
186+
if (id === nitroId) {
187+
// The id from native is same as the one we have,
188+
// so the view was unmounted before native manager was able to find it
189+
190+
// On slow devices, when we quickly mount and unmount the view,
191+
// the native manager may not have been able to find the view before the view was unmounted
192+
// This should really never happen, but it's better to be safe than sorry
193+
194+
// We don't throw an error here, because it's not an actual error.
195+
console.warn(
196+
'[ReactNativeVideo] VideoView was unmounted before native manager was able to find it. It can happen when the view is quickly mounted and unmounted.'
197+
);
198+
199+
return;
200+
}
201+
}
202+
203+
throw parsedError;
176204
}
177205
},
178206
[
@@ -182,6 +210,7 @@ const VideoView = React.forwardRef<VideoViewRef, VideoViewProps>(
182210
pictureInPicture,
183211
autoEnterPictureInPicture,
184212
resizeMode,
213+
nitroId,
185214
]
186215
);
187216

0 commit comments

Comments
 (0)