Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing a native stack navigation screen crashes the app on iOS if a modal is open #11201

Closed
4 of 14 tasks
juho-ylikyla opened this issue Feb 6, 2023 · 36 comments
Closed
4 of 14 tasks

Comments

@juho-ylikyla
Copy link

juho-ylikyla commented Feb 6, 2023

Current behavior

This issue is dependent on having react-native-modal library installed. I'm unsure where the root cause is, so I am reporting this bug in this repository.

The crashing did not occur in react-native version 0.70.x. The crash happens in version 0.71.2

If I have a react-native-modal open, calling navigation.dispatch(StackActions.replace(<route name>)); will crash the app

function HomeScreen({navigation}: {navigation: any}) {
  const [modalVisible, setModalVisible] = React.useState(false);
  return (
    <View
      style={{
        flex: 1,
        height: '100%',
        alignItems: 'center',
        justifyContent: 'center',
      }}>
      <Text>Home Screen</Text>
      <Button
        title="Open"
        onPress={() => {
          setModalVisible(true);
        }}
      />
      <Modal isVisible={modalVisible} useNativeDriver>
        <View style={{marginTop: '50%'}}>
          <Button
            title="Close"
            onPress={() => {
              // Removing this line does not affect the behavior
              setModalVisible(false);
              // This will crash the app
              navigation.dispatch(StackActions.replace('Home'));
            }}
          />
        </View>
      </Modal>
    </View>
  );
}

Error logged:

2023-02-06 13:25:35.130340+0200 AwesomeProject[53339:1560783] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally a view controller <RCTModalHostViewController: 0x108032dd0> that is already being presented by <UIViewController: 0x107f18b70>.'
*** First throw call stack:
(0x180adbc80 0x198301ee4 0x183228bec 0x1831971cc 0x182ee82c4 0x183029f1c 0x182fcae2c 0x18313134c 0x18304d738 0x104e89624 0x107da0700 0x107da1fc8 0x107db08ac 0x107db04fc 0x180a97034 0x180a54538 0x180a67194 0x1a159e988 0x18326aa88 0x183003fc8 0x10495d9ac 0x1079d04d0)
libc++abi: terminating with uncaught exception of type NSException
dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib
terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally a view controller <RCTModalHostViewController: 0x108032dd0> that is already being presented by <UIViewController: 0x107f18b70>.'

Expected behavior

Replacing the screen should not crash the app regardless if a react-native-modal is open or not

Reproduction

https://github.com/juho-ylikyla/rn-navigation-crash-repro

Platform

  • Android
  • iOS
  • Web
  • Windows
  • MacOS

Packages

  • @react-navigation/bottom-tabs
  • @react-navigation/drawer
  • @react-navigation/material-bottom-tabs
  • @react-navigation/material-top-tabs
  • @react-navigation/stack
  • @react-navigation/native-stack
  • react-native-tab-view
  • flipper-plugin-react-navigation

Environment

  • I've removed the packages that I don't use
package version
@react-navigation/native 6.1.3
@react-navigation/stack 6.3.12
@react-navigation/native-stack 6.9.9
react-native-safe-area-context 4.5.0
react-native-screens 3.19.0
react-native 0.71.2
@github-actions
Copy link

github-actions bot commented Feb 6, 2023

Hey! Thanks for opening the issue. The issue doesn't seem to contain a link to a repro (a snack.expo.dev link, a www.typescriptlang.org/play link or link to a GitHub repo under your username).

Can you provide a minimal repro which demonstrates the issue? Please try to keep the repro as small as possible and make sure that we can run it without additional setup.

A repro will help us debug the issue. The issue will be closed automatically after a while if you don't provide a repro.

@github-actions
Copy link

github-actions bot commented Feb 6, 2023

Couldn't find version numbers for the following packages in the issue:

  • @react-navigation/stack

Can you update the issue to include version numbers for those packages? The version numbers must match the format 1.2.3.

@github-actions
Copy link

github-actions bot commented Feb 6, 2023

The versions mentioned in the issue for the following packages differ from the latest versions on npm:

  • @react-navigation/stack (found: 6.9.9, latest: 6.3.12)

Can you verify that the issue still exists after upgrading to the latest versions of these packages?

@tmbradley
Copy link

I am getting the same issue when using react-native-modal on version 6.9.9 of @react-navigation/stack

@curthipster
Copy link

curthipster commented Mar 4, 2023

I have a very similar issue. It's crashing on iOS when calling navigation.replace while a modal is open. In my case, I'm using a stack navigator with presentation: 'transparentModal', and I do not have react-native-modal installed.

package version
@react-navigation/native 6.1.6
@react-navigation/native-stack 6.9.12
react-native 0.71.3

It does not crash with react-native 0.70.x

@hichemBAALI
Copy link

Any update here please?

@sebasg0
Copy link

sebasg0 commented Mar 21, 2023

I'm getting same issue any fix?

@curthipster
Copy link

@hichemBAALI and @sebasg0 do you have new architecture enabled? In my case, that's what's causing the crash: #11270. For now, I have reverted to not use the new architecture for iOS.

@tbhalke
Copy link

tbhalke commented Apr 25, 2023

Facing the same issue, I'm using the react-native-material-menu package which internally uses modal and I'm replacing the screen on menu-item press.

app crash

@Marmsilso
Copy link

Marmsilso commented May 9, 2023

I had the same issue, apparently it was happening only when the modal was being rendered

Since i did not found a good fix to this, i did short-term solution, i don't like it but it is what it is, perhaps it can help you in the meantime

I use the renderModal state to control if the modal is rendered, once you click the button to close the modal it changes the renderModal state which executes the useEffect hook triggering the replace

I am not that experienced in React Native, so any corrections to the code would be appreciated

function HomeScreen({navigation}: {navigation: any}) {
  const [modalVisible, setModalVisible] = React.useState(false);
  const [renderModal, setRenderModal] = React.useState(true);

  useEffect(() => {
    if(!renderModal){
       navigation.dispatch(StackActions.replace('Home'));
    }
  }, [renderModal])

  return (
    ...
      {renderModal &&
        <Modal isVisible={modalVisible} useNativeDriver>
          <View style={{marginTop: '50%'}}>
            <Button
              title="Close"
              onPress={() => {
                setModalVisible(false);
                setRenderModal(false);
              }}
            />
          </View>
        </Modal>
      }
     ...
  );
}

@ozberkjustlife
Copy link

ozberkjustlife commented May 29, 2023

This issue is still active. It started after we updated our react native version 0.71.4. We are getting same crashes. If a modal is open, and we try to go back and navigate to the same screen without closing modal app crash.

package version
@react-navigation/native 6.1.6
@react-navigation/stack 6.3.16
@react-navigation/native-stack 6.9.2
react-native-safe-area-context 4.4.1
react-native-screens 3.19.0
react-native: "0.71.4

@ChaseLavsa
Copy link

This is happening on RN 0.70.12 as well

@adrianha
Copy link

adrianha commented Jul 18, 2023

just opened a PR to fix this issue. This issue can be repro on react-native@0.72.3 as well. Can someone help me to confirm if the fix works?

@tarkcelk
Copy link

just opened a PR to fix this issue. This issue can be repro on react-native@0.72.3 as well. Can someone help me to confirm if the fix works?

thanks @adrianha it works 👍🏻

@github-actions
Copy link

Hey! Thanks for opening the issue. Seems that this issue is related to react-native-screens library which is a dependency of React Navigation. Can you also post your issue in this repo so that it's notified to the maintainers of that library? This will help us fix the issue faster since it's upto the maintainers of that library to investigate it.

@curthipster
Copy link

Hey! Thanks for opening the issue. Seems that this issue is related to react-native-screens library which is a dependency of React Navigation. Can you also post your issue in this repo so that it's notified to the maintainers of that library? This will help us fix the issue faster since it's upto the maintainers of that library to investigate it.

I opened this issue with them awhile back. No action yet.

@adrianha
Copy link

hey @curthipster, already opened one here

@chanphiromsok
Copy link

I got the issue with with replace screen is crash on IOS temporary solution I using navigate instead of replace

@oluwajuwon
Copy link

oluwajuwon commented Sep 14, 2023

I am using react-native-modal
I got this issue when i tried to reset my navigation while i triggered the modal to close, I fixed it by handling the navigation reset with the onModalHide prop, so it calls the method to reset the navigation only when the modal has been completely dismissed.

@hacnam0306
Copy link

Put the navigation function into setTimeOut or delay (lodash ) function , it will oke
onClose(); // close modal function
// replace function
delay(() => {
replace(RouteName.CHAT_DETAIL, {
roomId: +roomId,
type: type || EChatRoomType.FRIEND,
});
}, 500);

@salman-ar-sar
Copy link

setTimeout is fixing the issue but has some weird different issues. Did anyone any other workarounds or fix?

@srdrbdrd
Copy link

srdrbdrd commented Nov 8, 2023

Calling navigation.reset() function when onModalHide works for temporary solution till fixed

@pfcodes
Copy link

pfcodes commented Nov 22, 2023

This is happening when I'm trying to replace a screen inside of a top-level stack navigator which is presented in a full screen modal.

As a workaround I used navigation.push() to get to what should be the replaced screen (which I defined with gesturesEnabled: false.

Also used navigation.getParent()?.goBack() to dismiss the top-level stack navigator modal.

@mathbalduino
Copy link

Still happening. I fixed it by using a setTimeout(), but it is not the ideal...

@sasweb
Copy link

sasweb commented Dec 22, 2023

Can confirm it's still happening in RN 0.72.4 with an open modal.
Any maintainers reading this? It's already open 10+ months...

@AminDannak
Copy link

Can confirm it's still happening in RN 0.72.4 with an open modal. Any maintainers reading this? It's already open 10+ months...

same on RN 0.72.6

@salman-ar-sar
Copy link

I tried on RN 0.73.0 and the same issue is present. I hope some of the maintainers are looking into this.

@eidan66
Copy link

eidan66 commented Feb 26, 2024

I having this issue also with the navigation.reset option.
RN72.x

Any solution for this? :(

@Vrunalbenke
Copy link

@oluwajuwon can't thank you more, your solution solve my IOS Crash issue

@devaamir
Copy link

I am using react-native-modal I got this issue when i tried to reset my navigation while i triggered the modal to close, I fixed it by handling the navigation reset with the onModalHide prop, so it calls the method to reset the navigation only when the modal has been completely dismissed.

this method work for us. but its not a proper way 🚀🚀

@boiboif
Copy link

boiboif commented Apr 16, 2024

Same issue,Temporary solution is using setTimeout.

@Sepudev
Copy link

Sepudev commented May 6, 2024

Same crash here, im using 0.73.6 and still crashing, provisional solution is using a timeout :(

@satya164
Copy link
Member

satya164 commented May 7, 2024

As per software-mansion/react-native-screens#1832 (comment) this seems to a bug in React Native. So I'm closing this.

Please open an issue in React Native repo.

@satya164 satya164 closed this as completed May 7, 2024
Copy link

github-actions bot commented May 7, 2024

Hey! This issue is closed and isn't watched by the core team. You are welcome to discuss the issue with others in this thread, but if you think this issue is still valid and needs to be tracked, please open a new issue with a repro.

@syedumerali101
Copy link

Import Modal from react native instead of importing it from react-native-modal.

Copy link

Hey! This issue is closed and isn't watched by the core team. You are welcome to discuss the issue with others in this thread, but if you think this issue is still valid and needs to be tracked, please open a new issue with a repro.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests