简体   繁体   中英

React Native BottomTabNavigator navigation not working

I created a BottomTabNavigator app with expose, it's my first project with react-native. On the last tab I have a FlatList with a few elements which should open different Views outside the TabNavigation. But when I click on a item in the ListView, I get the following error: TypeError: undefined is not an object (evaluating 'item.navigation.navigate'. This also happens when I try to do it with props, but props also won't work properly in my project.

I'm trying to fix this for two days and I don't know any further. Can anyone please help me with this? In addition, has someone a good guide for navigation and props?

App.tsx

export default function App() {
  const isLoadingComplete = useCachedResources();
  const colorScheme = useColorScheme();

  if (!isLoadingComplete) {
    return null;
  } else {
    return (
      <SafeAreaProvider>
        <Navigation colorScheme={colorScheme} />
        <StatusBar />
      </SafeAreaProvider>
    );
  }
}

index.tsx

export default function Navigation({ colorScheme }: { colorScheme: ColorSchemeName }) {
  return (
    <NavigationContainer
      linking={LinkingConfiguration}
      theme={colorScheme === 'dark' ? DarkTheme : DefaultTheme}>
      <RootNavigator />
    </NavigationContainer>
  );
}

const Stack = createStackNavigator<RootStackParamList>();

function RootNavigator() {

  return (
    <Stack.Navigator screenOptions={{ headerShown: false }}>
      <Stack.Screen name="Root" component={BottomTabNavigator} />
      <Stack.Screen name="WebViewImprint" component={WebViewImprint} />
      <Stack.Screen name="MenuFlatList" component={MenuFlatList} />

      <Stack.Screen name="NotFound" component={NotFoundScreen} options={{ title: 'Oops!' }} />
    </Stack.Navigator>
  );
}

BottomTabNavigator.tsx

const BottomTab = createBottomTabNavigator<BottomTabParamList>();

export default function BottomTabNavigator() {
  const colorScheme = useColorScheme();

  return (
      { .... }
      <BottomTab.Screen
        name="TabMenu"
        component={TabMenuNavigator}
        options={{
          tabBarLabel: "Menü",
          tabBarIcon: ({ color }) => <TabBarIcon name="ios-code" color={color} />,
        }}
      />
    </BottomTab.Navigator>
  );
}

function TabBarIcon(props: { name: string; color: string }) {
  return <Ionicons size={30} style={{ marginBottom: -3 }} {...props} />;
}

const TabMenuStack = createStackNavigator<TabMenuParamList>();

function TabMenuNavigator() {
  const colorScheme = useColorScheme();

  return (
    <TabMenuStack.Navigator>
      <TabMenuStack.Screen
        name="TabMenu"
        component={TabMenu}
        options={{ 
          header: props => <StacknavigatorImage />,
          headerStyle: {
            backgroundColor: Colors[colorScheme].barBackgroundColor
          }
        }}
      />
    </TabMenuStack.Navigator>
  );
}

MenuFlatListComponent.tsx

const Item = ({ title } : {title: any}) => (
     <View style={styles.item}>
          <Text style={styles.title}>{title}</Text>
     </View>
);

export default class MenuFlatList extends React.Component {

     render(){

          return (
               <SafeAreaView style={styles.container}>
                    <FlatList style={styles.flatList}
                    data={DATA}
                    renderItem={({item} : {item: any}) => {
                         return (
                              <TouchableWithoutFeedback onPress={() => {
                                   item.navigation.navigate('WebViewImprint');
                              }}>
                                   <View style={styles.item}>
                                             <Image source={item.icon} style={styles.icon} />
                                             <Item title={item.title} />
                                   </View>
                              </TouchableWithoutFeedback>
                         );}
                         
                    }
                    keyExtractor={item => item.id}
                    />
               </SafeAreaView>
          );
     }
}

You are trying to access navigation which is passed to the MenuFlatList component as a prop you should do like below

  <TouchableWithoutFeedback onPress={() => {
         this.props.navigation.navigate('WebViewImprint');
   }}>

Any screen component in the navigation will receive a 'navigation' props which you can use to navigate to other screens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM