question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

enableFreeze cause useIsFocused stop working

See original GitHub issue

Description

In bottom tabs, when enabling enableFreeze, useIsFocused is not triggered when screen blur.

Screenshots

Steps To Reproduce

  1. Create tabs with 2 screens Home and Settings
  2. In Settings, add const isFocused = useIsFocused()
  3. Navigate to Settings screen, isFocused updated to true (this is correct)
  4. Navigate back to Home, isFocused still true

Expected behavior

At step 4, isFocused in Settings should be false

Actual behavior

At step 4, isFocused in Settings is true

Reproduction

Platform

I just checked on iOS, not sure about other platforms

  • iOS
  • Android
  • Web
  • Windows
  • tvOS

Workflow

  • Managed workflow
  • Bare workflow

Package versions

package version
react-native 0.66.1
@react-navigation/native 6.0.6
@react-navigation/native-stack 6.2.5
@react-navigation/bottom-tabs 6.0.9
react-native-screens 3.9.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
kacperkapusciakcommented, Nov 24, 2021

Hi @Titozzz, @r0b0t3d, @nandorojo, @marhaupe, and @tomasmozeris

Thank you so much for participating in this discussion. It helped a lot!

I think we’ve finally figured out how to overcome this issue and the solution is quite simple. We disable freeze for the first render so the useIsFocused can correctly pick up that screens have changed.

Could you guys confirm the solution works by testing the code in your app eg. by installing react-native-screens directly from the branch

yarn add git+https://github.com/software-mansion/react-native-screens#@kacperkapusciak/disable-freeze-for-first-render

or swapping the whole index.native.tsx file in your node_modules/react-native-screens?

That would be amazing! 🙌 Thanks!

1reaction
amadeuscommented, Nov 8, 2022

Looking at that code for the delayed freeze, it’s actually not concurrent safe (executing side effects in a render function, that could get thrown away). A more proper fix would be something like this:

diff --git a/node_modules/react-native-screens/src/index.native.tsx b/node_modules/react-native-screens/src/index.native.tsx
index edcd032..78aba2b 100644
--- a/node_modules/react-native-screens/src/index.native.tsx
+++ b/node_modules/react-native-screens/src/index.native.tsx
@@ -1,4 +1,4 @@
-import React from 'react';
+import React, {useEffect} from 'react';
 import {
   Animated,
   Image,
@@ -152,13 +152,11 @@ function DelayedFreeze({ freeze, children }: FreezeWrapperProps) {
   // flag used for determining whether freeze should be enabled
   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.
+  useEffect(() => {
     setImmediate(() => {
       setFreezeState(freeze);
     });
-  }
+  }, [freeze])
 
   return <Freeze freeze={freeze ? freezeState : false}>{children}</Freeze>;
 }

FWIW: This does fix the problem for us with Native Stacks and useIsFocused firing properly

Read more comments on GitHub >

github_iconTop Results From Across the Web

useIsFocused - React Navigation
The library exports a useIsFocused hook to make this easier: ... This might cause lags during the animation if your screen is heavy....
Read more >
Why is my tab re-rendering (i'm using useEffect on ... - Reddit
I think the tabs are different component, so you can check it out useIsFocused() from react-navigation. const isFocused = useIsFocused();. then ...
Read more >
React-Navigation useIsFocused always returns true, and I can ...
I have pretty simple use case for this, Basically if the screen is focused I want this function to run, if not don't...
Read more >
react-native-screens - npm
This project aims to expose native navigation container components to React Native. It is not designed to be used as a standalone library ......
Read more >
react-native-screens - bytemeta
enableFreeze cause useIsFocused stop working ... detachInactiveScreens={true} cause onScroll of previous tab screen triggered in react navigation v6 bottom ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found