0

Under "react-navigation/stack" of react-navigation 6, It did well working all of function like useRoute, useNavigation..etc

But, I changed stack navigation to react-navigation/native-stack from react-navigation/stack.

native-stack does not found a route object in "GroupHeaders component"

Below my code.

  1. Root navigation
import { createNativeStackNavigator, NativeStackHeaderProps } from "@react-navigation/native-stack";

const RootNavigation = createNativeStackNavigator();
    <NavigationContainer>
        <RootNavigation.Navigator>
          <RootNavigation.Screen
            name="Main-root"
            component={TabNavigation}
            options={{ headerShown: false }}
          />
        
          <RootNavigation.Screen
            name="PostDetail-root"
            component={PostDetailContainer}
            options={{ header: () => <GroupHeaders /> }}. // ***** HERE
          />
         // ...other screen...

  1. Tab navigation
      <Tab.Navigator
        initialRouteName="home-tab"
        //...
        >
        <Tab.Screen
          name="home-tab"
          component={HomeNavigation}
          options={{
            headerShown: false,
            tabBarLabel: "Home",
            //...
          }}
        />
  1. GroupHeaders component
const GroupHeaders = ({}) => {
  const route = useRoute(); //**** HERE, RN occurs error message of "~not find a route object~~" 
  // ... other code

rudenick
  • 154
  • 1
  • 1
  • 10

1 Answers1

1

you can use option paramter

reference document.

<RootNavigation.Screen
    name="PostDetail-root"
    component={PostDetailContainer}
    options={({ route }) => ({
    tabBarLabel: (props) => <GroupHeaders route={route} {...props}  />,
  })}/> // ***** HERE
/>
  • use
  function GroupHeaders({route}) {
    console.log("route1",route);
    return (
        <Text>{route.name}</Text>
    )
  }
hong developer
  • 13,291
  • 4
  • 38
  • 68
  • Thank you for your answer. But, Your answer not solved my problem. RN presents the error message like 'could not find a route object'. @hong developer – rudenick Jan 21 '22 at 01:34
  • @rudenick I revised my answer. I made a test for you. https://snack.expo.dev/nS7BuW3E18 – hong developer Jan 26 '22 at 10:51