skip to Main Content

This is the full error:

Error: Looks like you have nested a NavigationContainer inside another. Normally you need only one container at the root of the app, so this was probably an error. If this was intentional, pass ‘independent={true}’ explicitly. Note that this will make the child navigators disconnected from the parent and you won’t be able to navigate between them.

I don’t see why I am getting this error as I only have one NavigationContainer

index.js

import { useState, useEffect } from 'react';
import { View, ScrollView, SafeAreaView, Text, Button } from 'react-native';
import { useRouter } from 'expo-router';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack'

import Home from './home';
import styles from '../myComponents/common/header/header/header.style';
import { COLORS, icons, images, FONT, SIZES } from '../constants';
import { ScreenHeaderBtn, TimeSlider, SessionBtn } from '../myComponents';

const Stack = createNativeStackNavigator();

function App() {
    const router = useRouter();

    return (
        <NavigationContainer>
            <Stack.Navigator>
                <Stack.Screen name="Home" component={Home} />
            </Stack.Navigator>
        </NavigationContainer>
    );
};

export default App;

home.js

import { useState, useEffect } from 'react';
import { View, ScrollView, SafeAreaView, Text, Button } from 'react-native';
import { Stack, useRouter } from 'expo-router';

import styles from '../myComponents/common/header/header/header.style';
import { COLORS, icons, images, FONT, SIZES } from '../constants';
import { ScreenHeaderBtn, TimeSlider, SessionBtn } from '../myComponents';

const Home = () => {
    const router = useRouter();

    return (
        <SafeAreaView style={{ flex: 1, backgroundColor: COLORS.lightBeige }}>
            <Stack.Screen
                options={{
                    headerStyle: { backgroundColor: COLORS.lightBeige },
                    headerShadowVisible: true,
                    headerLeft: () => (
                        <ScreenHeaderBtn iconUrl={icons.menu} dimension="60%" />
                    ),
                    headerTitle: () => (
                        <Text style={styles.headerStyle}>
                            Flowtime
                        </Text>
                    ),
                    headerTitleAlign: 'center',
                }}
            />
            <ScrollView showsVerticalScrollIndicator={false}>
                <View style={{ flex: 1, padding: SIZES.medium, }}>

                    <View style={{ alignItems: 'center', padding: SIZES.xLarge }}>
                        <SessionBtn />
                    </View>
                    <View style={{ alignItems: 'center', padding: SIZES.xLarge }}>
                        <TimeSlider />
                    </View>

                </View>
            </ScrollView>
        </SafeAreaView>
    );
};

export default Home;

_layout.js

import { Stack } from 'expo-router';
import { useCallback } from 'react';
import { useFonts } from 'expo-font';
import * as SplashScreen from 'expo-splash-screen';

SplashScreen.preventAutoHideAsync();

const Layout = () => {
    const [fontsLoaded] = useFonts({
        MontBold: require('../assets/fonts/Montserrat-Bold.ttf'),
        MontMed: require('../assets/fonts/Montserrat-Medium.ttf'),
        MontReg: require('../assets/fonts/Montserrat-Regular.ttf'),
    })

    const onLayoutRootView = useCallback(async () => {
        if(fontsLoaded) {
            await SplashScreen.hideAsync();
        }
    }, [fontsLoaded]
    )

    if(!fontsLoaded) return null;

    return <Stack onLayout={onLayoutRootView}/>;
}

export default Layout;

I tried moving the Stack.Screen header in home.js around into the NavigationContainer in index.js but then I get another error that the Stack.Screen doesn’t have a name or and doesn’t have a proper component. I’m not sure how else to fix this error as I only have one NavigationContainer. What’s even more confusing is that it looks like the React navigation documentation as well.

2

Answers


  1. You can set options in index.js. You can refer here.

    https://reactnavigation.org/docs/screen-options-resolution/

    https://reactnavigation.org/docs/screen-options/

    <NavigationContainer>
        <Stack.Navigator>
            <Stack.Screen name="Home" component={Home} options={{
                headerStyle: { backgroundColor: COLORS.lightBeige },
                headerShadowVisible: true,
                headerLeft: () => (
                    <ScreenHeaderBtn iconUrl={icons.menu} dimension="60%" />
                ),
                headerTitle: () => (
                    <Text style={styles.headerStyle}>
                        Flowtime
                    </Text>
                ),
                headerTitleAlign: 'center'
            }} />
        </Stack.Navigator>
    </NavigationContainer>
    
    Login or Signup to reply.
  2. Here is the complete answer.

    this should be your index.js File

    import React from 'react';
    import {NavigationContainer} from '@react-navigation/native';
    import {createNativeStackNavigator} from '@react-navigation/native-stack';
    import Home from './home';
    
    const Stack = createNativeStackNavigator();
    
    function App() {
      return (
        <NavigationContainer>
          <Stack.Navigator>
            <Stack.Screen
              name="Home"
              component={Home}
              options={{
                headerTitleAlign: 'center',
                headerTintColor: '#0090FF',
                headerTitleStyle: {
                  color: '#000000',
                  fontSize: 14,
                },
                headerStyle: {
                  backgroundColor: '#FFFFFF',
                },
                headerShown: true,
                title: 'Home Screen',
              }}
            />
          </Stack.Navigator>
        </NavigationContainer>
      );
    }
    
    export default App;
    

    Here is your Home.js File

    import {SafeAreaView, ScrollView, View} from 'react-native';
    import {COLORS, SIZES} from '../constants';
    import {SessionBtn, TimeSlider} from '../myComponents';
    import React from 'react';
    const Home = () => {
      return (
        <SafeAreaView style={{flex: 1, backgroundColor: COLORS.lightBeige}}>
          <ScrollView showsVerticalScrollIndicator={false}>
            <View style={{flex: 1, padding: SIZES.medium}}>
              <View style={{alignItems: 'center', padding: SIZES.xLarge}}>
                <SessionBtn />
              </View>
              <View style={{alignItems: 'center', padding: SIZES.xLarge}}>
                <TimeSlider />
              </View>
            </View>
          </ScrollView>
        </SafeAreaView>
      );
    };
    
    export default Home;
    

    and here is your _layout.js File

    import {useFonts} from 'expo-font';
    import {Stack} from 'expo-router';
    import * as SplashScreen from 'expo-splash-screen';
    import {useCallback} from 'react';
    
    SplashScreen.preventAutoHideAsync();
    
    const Layout = () => {
      const [fontsLoaded] = useFonts({
        MontBold: require('../assets/fonts/Montserrat-Bold.ttf'),
        MontMed: require('../assets/fonts/Montserrat-Medium.ttf'),
        MontReg: require('../assets/fonts/Montserrat-Regular.ttf'),
      });
    
      const onLayoutRootView = useCallback(async () => {
        if (fontsLoaded) {
          await SplashScreen.hideAsync();
        }
      }, [fontsLoaded]);
    
      if (!fontsLoaded) return null;
    
      return <Stack onLayout={onLayoutRootView} />;
    };
    
    export default Layout;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search