Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

React Navigation throws 'you have nested a NavigationContainer inside another'

ErrorError: Looks like you have nested a 'NavigationContainer' inside another.

Short answer

React Navigation allows one NavigationContainer for the whole app. The error means a second container renders somewhere inside the first one's tree. Find it and delete it, because nested navigators go inside screens and need no container. Only if you truly want a separate tree, wrap that container in NavigationIndependentTree.

  • Applies to React Navigation 7
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List every file that renders a NavigationContainer. The app should have exactly one, in the root component. Any other hit is a suspect.

    grep -rn "NavigationContainer" --include=*.tsx --include=*.jsx --include=*.js --include=*.ts app src components screens 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1More than one file in the grep output renders <NavigationContainer>, and the error appears as soon as that screen mounts.A screen or child component wraps its own NavigationContainerFix
    2. 2The nested container is deliberate, for example a self-contained flow with its own history, and the error text itself suggests NavigationIndependentTree.You want a second, fully separate navigation treeFix

What does the error look like?

  • Error: Looks like you have nested a 'NavigationContainer' inside another.
  • If this was intentional, wrap the container in 'NavigationIndependentTree' explicitly.

Cause 1 of 2

A screen or child component wraps its own NavigationContainer

1Check

Open each extra hit. A common source is a screen file copied from a tutorial or an example that carried its own App wrapper, so the screen renders a container while already inside the root one.

// Root: this is the only place a container belongs
<NavigationContainer>
  <RootStack />
</NavigationContainer>

2Fix

Delete the extra container and return the navigator directly. A navigator rendered inside a screen of another navigator is how nesting works, and it needs no container of its own.

// before: inside HomeTabs.tsx
// return (<NavigationContainer><Tab.Navigator>...</Tab.Navigator></NavigationContainer>);

// after
export function HomeTabs() {
  return (
    <Tab.Navigator>
      <Tab.Screen name="Feed" component={FeedScreen} />
    </Tab.Navigator>
  );
}

// and the stack registers it as a screen
<Stack.Screen name="Home" component={HomeTabs} options={{ headerShown: false }} />

3Verify

Reload the app. The red screen is gone.

npx expo start --clear

Confirmed by reactnavigation.org

Did this fix it?

Cause 2 of 2

You want a second, fully separate navigation tree

1Check

Decide whether that flow really needs to be disconnected from the parent. The docs say an independent tree cannot navigate to or from the rest of the app, so an ordinary nested navigator is usually what you want.

grep -rn "NavigationIndependentTree" src app

2Fix

Wrap only that inner container in NavigationIndependentTree, imported from @react-navigation/native.

import { NavigationContainer, NavigationIndependentTree } from '@react-navigation/native';

export function EmbeddedFlow() {
  return (
    <NavigationIndependentTree>
      <NavigationContainer>
        <FlowStack />
      </NavigationContainer>
    </NavigationIndependentTree>
  );
}

3Verify

The error no longer appears. Remember that navigate() calls inside the flow cannot reach screens outside it.

npx expo start --clear

Confirmed by reactnavigation.org

Did this fix it?

How do I stop it coming back?

  • Keep the single NavigationContainer in the root component and nowhere else.
  • Build each part of the app as a navigator or a screen, never as a small app with its own wrapper.
  • Search for NavigationContainer when you paste a screen or example from another project.
  • Use NavigationIndependentTree only for flows that must be cut off from the main navigation.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.