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.

Modal doesn't close when navigating to another tab.

See original GitHub issue

Hey there, I am using the native-stack.

I have a bit of an issue. I have stacks nested inside tabs. I’m trying to close a modal when I jump to another tab. However, the modal stays open when I jump from one tab to another.

Current Behavior

Tab1 opens a modal. User places an order. This needs to send the user to Tab2 (where I can see my orders.)

- Tab1
  - Stack
      - Screen
      - Modal
- Tab 2 (Orders)

Illustration

// Modal inside of Tab1
import { TabActions, useNavigation } from '@react-navigation/native';

const { navigate, dispatch } = useNavigation()

const placeOrder = async () => {
  const success = await order()
  // ok, let's now close this modal and send the user to Tab2
  dispatch(TabActions.jumpTo('Orders')) // jumps tabs, but the modal stays open
}

I would expect this to do two things: 1) close the modal, 2) jump to tab 2. It does indeed jump to tab 2. However, the modal stays open. I’m not really sure what the proper behavior is, or how to handle this.

I’ve also tried…

I tried using popToTop() before jumpTo('Orders'), but then it never changes tabs. Same goes for goBack().

// tab1 modal
import { StackActions, useNavigation } from '@react-navigation/native';

const { navigate, dispatch } = useNavigation()

const placeOrder = async () => {
  const success = await order()
  // ok, let's now send the user to tab2
  dispatch(StackActions.popToTop()) // pops to main tab screen
  dispatch(TabActions.jumpTo('Orders')) // this never happens
}

In this case, it does indeed pop to the main tab navigator, but it never goes to the Orders tab.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
miticouscommented, May 20, 2021

I solved this issue by adding a navigation event listener to control when modal show up or not with some flag to prevent modal shown on screen load at the first time… Example:

  React.useEffect(() => {
    const unsubscribe = navigation.addListener('blur', () => {
      setShowSuccessModal(false);
    });

    return unsubscribe;
  }, [navigation]);

  React.useEffect(() => {
    const unsubscribe = navigation.addListener('focus', () => {
      if (switcherScreenIndex === Screens.USER_DEFINE_PASSWORD) {
        setShowSuccessModal(true);
      }
    });

    return unsubscribe;
  }, [navigation, switcherScreenIndex]);
1reaction
nandorojocommented, Nov 25, 2020

Hey thanks for following up. I haven’t yet, but I can close this for now while I keep experimenting. I’ll report back if I have new info. Thanks again your help, really appreciate it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react native - Dismiss modal when navigating to another screen
I have an App with Home Screen, in this screen I'm rendering a Modal which opens on button ...
Read more >
Modal · Bootstrap v5.2
Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time. Nested modals aren't supported...
Read more >
<Modal/> Component - React-Bootstrap
Modals are unmounted when closed. Bootstrap only supports one modal window at a time. Nested modals aren't supported, but if you really need...
Read more >
How to close modal in Bootstrap - code helpers
There are few ways to close modal in Bootstrap: click on a backdrop, close icon, or close button. You can also use JavaScript...
Read more >
How to get a React Bootstrap Modal to open and close using a ...
Now, when a user clicks the Tab marked Show the Modal! , they'll open up the Modal, and when they click the x...
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