Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Looks like you have nested a 'NavigationContainer' inside another. Normally you need only one container at the root of the app

I followed the docs of React 5 for Drawer Navigation in react native but getting this error. Here is my Code

import React from 'react'
import {
    View,
    Button,
    Text,
} from 'react-native'

import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';

import Screen1 from './DrawerScreens/Screen1';
import Screen2 from './DrawerScreens/Screen2';
import Screen3 from './DrawerScreens/Screen3';

const Drawer = createDrawerNavigator();

function Navigations()
{
    return(
        <NavigationContainer>
            <Drawer.Navigator initialRouteName="Home">
                <Drawer.Screen name="Home" component={Screen1} />
                <Drawer.Screen name="Settings" component={Screen2} />
                <Drawer.Screen name="Contacts" component={Screen3} />
            </Drawer.Navigator>
        </NavigationContainer>
    );
}

export default Navigations;

I am new to react native, so don't know what to do

like image 520
Annas Bin Waheed Avatar asked May 08 '20 10:05

Annas Bin Waheed


People also ask

How many containers should be at the root of the app?

Normally you need only one container at the root of the app – JavaScript Error: Looks like you have nested a ‘NavigationContainer’ inside another. Normally you need only one container at the root of the app

How many navigation containers do I need for my App?

Normally you need only one container at the root of the app - Stack Overflow Error: Looks like you have nested a 'NavigationContainer' inside another. Normally you need only one container at the root of the app Bookmark this question. Show activity on this post.

What are some examples of nested stack navigators?

For example, when you press the back button when inside a screen in a nested stack navigator, it'll go back to the previous screen inside the nested stack even if there's another navigator as the parent. For example, specifying a title option in a screen nested in a child navigator won't affect the title shown in a parent navigator.

Why is my navigationcontainer not importing screens?

If you have only one NavigationContainer in your app, please check if you're importing screens correctly. In my case, I've mistakenly imported the entry route file which contains NavigationContainer itself. This can happen if you're using same file names for entry route files (e.g. Root.tsx or Main.tsx) and let IDE import files automatically.


Video Answer


2 Answers

You only need to declare one < NavigationContainer > in the top component, example:

function SecondComponent() {
  return (
    <Tab.Navigator>
      <Tab.Screen name="Feed" component={Feed} />
      <Tab.Screen name="Messages" component={Messages} />
    </Tab.Navigator>
  );
}

function FirstComponent() {
  return (
    <NavigationContainer> {/* this is the only NavigationContainer */}
      <Stack.Navigator>
        <Stack.Screen name="Home" component={Home} />
        <Stack.Screen name="Profile" component={Profile} />
        <Stack.Screen name="Settings" component={Settings} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}
like image 102
Gonzalo Quispe Fernandez Avatar answered Sep 16 '22 14:09

Gonzalo Quispe Fernandez


pass independent={true} to both of the Navigation container .

<NavigationContainer independent={true}>
</NavigationContainer>

But you will not be able to navigate between the screens of two separate navigation container

If you want to navigate between them then you have to maintain single navigation container.

like image 32
Manoj D Bhat Avatar answered Sep 17 '22 14:09

Manoj D Bhat