[英]React Native: createBottomTabNavigator is not working
I placed all of my navigation inside a navigation/MealsNavigator.js
file:我将所有导航都放在了一个
navigation/MealsNavigator.js
文件中:
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import CategoriesScreen from '../screens/CategoriesScreen';
import CategoryMealsScreen from '../screens/CategoryMealsScreen';
import MealDetailScreen from '../screens/MealDetailScreen';
import FavoritesScreen from '../screens/FavoritesScreen';
import HeaderButton from '../components/HeaderButton';
import { HeaderButtons, Item } from 'react-navigation-header-buttons';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { CATEGORIES } from '../data/dummy-data';
import Colors from '../constants/colors';
const MealsNav = createStackNavigator();
const MealsNavigator = () => {
return (
<NavigationContainer>
<MealsNav.Navigator
mode="modal"
screenOptions={{
headerStyle: {
backgroundColor: Colors.primaryColor,
},
headerTintColor: '#fff',
headerTitleStyle: {
fontSize: 17
}
}}
>
<MealsNav.Screen
name="Categories"
component={CategoriesScreen}
options={{
title: 'Meals Categories'
}}
/>
<MealsNav.Screen
name="CategoryMeals"
component={CategoryMealsScreen}
options={({ route }) => {
const catId = route.params.categoryId;
const selectedCategory = CATEGORIES.find((cat) => cat.id === catId);
return {
title: selectedCategory.title,
};
}}
/>
<MealsNav.Screen
name="MealDetail"
component={MealDetailScreen}
options={{
title: 'Meal Detail',
headerRight: () => (
<HeaderButtons HeaderButtonComponent={HeaderButton}>
<Item
title='Favorite'
iconName='ios-star'
onPress={() => console.log('Mark as the favorite')}
/>
</HeaderButtons>
),
}}
/>
</MealsNav.Navigator>
</NavigationContainer>
);
};
const MealsFavTabNavigator = createBottomTabNavigator();
const MealsTabNav = () => {
return (
<MealsFavTabNavigator.Navigator>
<MealsFavTabNavigator.Screen name="Meals" component={MealsNavigator} />
<MealsFavTabNavigator.Screen name="Favorites" component={FavoritesScreen} />
</MealsFavTabNavigator.Navigator>
);
};
export default MealsTabNav;
I tried to follow the documentation here: https://reactnavigation.org/docs/bottom-tab-navigator/我试着按照这里的文档: https://reactnavigation.org/docs/bottom-tab-navigator/
And the responsible for the bottom tabs is this part of the code:负责底部选项卡的是这部分代码:
const MealsFavTabNavigator = createBottomTabNavigator();
const MealsTabNav = () => {
return (
<MealsFavTabNavigator.Navigator>
<MealsFavTabNavigator.Screen name="Meals" component={MealsNavigator} />
<MealsFavTabNavigator.Screen name="Favorites" component={FavoritesScreen} />
</MealsFavTabNavigator.Navigator>
);
};
export default MealsTabNav;
At first, I thought I am following through the docs until it threw me an error:起初,我以为我正在阅读文档,直到它给我一个错误:
could not register the navigator. Have you wrapped your app with "NavigationContainer"?
I included all the necessary files but still I am getting these errors.我包含了所有必要的文件,但我仍然收到这些错误。 Any idea how to fix this?
知道如何解决这个问题吗?
Beginner here.初学者在这里。
Try including:尝试包括:
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
You should always use NavigationContainer to wrap the top level navigator here you are using it in MealsNavigator which is wrapped inside the MealsTabNav您应该始终使用 NavigationContainer 来包装顶级导航器,您在 MealsNavigator 中使用它,该导航器包装在 MealsTabNav 内
so remove the NavigationContainer in MealsNavigator and change the MealsTabNav like below.所以删除 MealsNavigator 中的 NavigationContainer 并更改 MealsTabNav 如下所示。
const MealsTabNav = () => {
return (
<NavigationContainer>
<MealsFavTabNavigator.Navigator>
<MealsFavTabNavigator.Screen name="Meals" component={MealsNavigator} />
<MealsFavTabNavigator.Screen name="Favorites" component={FavoritesScreen} />
</MealsFavTabNavigator.Navigator>
</NavigationContainer>
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.