Skip to content

React Freeze Causes Playback to Continue (react-native-video) #1403

Open
@swushi

Description

Hi! 👋

Firstly, thanks for your work on this project!

Today I used patch-package to patch react-native-screens@3.13.1 for the project I'm working on.

I work on a social media app that has a list of Audio/Video components. We stop playback for those components based on a couple of conditions, one of them being lack of focus on the screen. When trying to incorporate react-freeze, the component would be frozen, with playback still going, before knowing it had lost focus. Thus we would have many pieces of playback playing concurrently.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-screens/src/index.native.tsx b/node_modules/react-native-screens/src/index.native.tsx
index 35ce64c..ff1bdb7 100644
--- a/node_modules/react-native-screens/src/index.native.tsx
+++ b/node_modules/react-native-screens/src/index.native.tsx
@@ -167,11 +167,13 @@ function DelayedFreeze({ freeze, children }: FreezeWrapperProps) {
   const [freezeState, setFreezeState] = React.useState(false);
 
   if (freeze !== freezeState) {
-    // setImmediate is executed at the end of the JS execution block.
-    // Used here for changing the state right after the render.
-    setImmediate(() => {
+    // setTimeout is executed at the end of the JS execution block.
+    // Used here for changing the state with a slight delay.
+    // This timeout allows playback from audio/videos to stop playing before being frozen with playback.
+    setTimeout(() => {
       setFreezeState(freeze);
-    });
+    }, 100);
   }
 
   return <Freeze freeze={freeze ? freezeState : false}>{children}</Freeze>;

This issue body was partially generated by patch-package.

Metadata

Assignees

No one assigned

    Labels

    Missing infoThe user didn't precise the problem enoughRepro providedA reproduction with a snack or repo is provided

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions