导航类型定义如下,当我从 例如 导航AOne到时BTwo,控制台id:99 日志props.route.params显示正确的信息。但props.route.params.id抛出类型错误
类型错误:未定义不是对象(正在评估“props.route.params.id”)
// navigation related imports in all components
import {BottomTabScreenProps} from '@react-navigation/bottom-tabs';
import {CompositeScreenProps, NavigatorScreenParams} from '@react-navigation/core';
import {StackScreenProps} from '@react-navigation/stack';
// type defenitions
export type StackOneParams = {
AOne:undefined,
BOne: undefined,
// some other screens
};
export type StackTwoParams = {
ATwo: undefined;
BTwo:{id:number};
// some other screens
};
export type TabParams = {
StackOne: NavigatorScreenParams<StackOneParams>;
StackTwo: NavigatorScreenParams<StackTwoParams>;
// ... some other stacks each represent a …Run Code Online (Sandbox Code Playgroud) react-native react-navigation react-navigation-stack react-navigation-bottom-tab react-navigation-v6
当我的应用程序打开时,我有一个@react-navigation/bottom-tabs导航器,其内容如下:
<Tab.Navigator
tabBarOptions={{
activeTintColor: '#77dd77',
inactiveTintColor: 'gray',
}}
tabBar={props => <MyTabBar {...props} />}
backBehavior={"history"}
>
<Tab.Screen
name="Home"
component={Home}
options={{ title: 'Home' }}
/>
<Tab.Screen
name="Orders"
component={Orders}
options={{ title: 'Orders' }}
/>
<Tab.Screen
name="Profile"
component={Profile}
options={{ title: 'Profile' }}
/>
</Tab.Navigator>
Run Code Online (Sandbox Code Playgroud)
我的代码中有一个BackHandler,可以在从主页按下后退按钮时使应用程序退出。一切都很好,我已经检查了按下后退按钮时是否会调用后处理程序。
但是,当我切换到任何其他选项卡,然后返回主页并按返回退出应用程序时,后台处理程序停止工作并且应用程序显示错误“任何导航器均未处理操作“GO_BACK”。是否有任何屏幕可以返回到?”
这是仅用于开发的警告,但在签名版本中,应用程序不会显示任何错误,甚至不会退出。
我该如何解决这个“GO_BACK”操作?
android react-native react-navigation react-navigation-bottom-tab
我正在使用materialTopTabs,似乎这会在安装后加载导航器中的所有屏幕。我有一个屏幕列表,其中有一个带有 2 个屏幕的选项卡导航器:Posts和Users。这两个屏幕都取决于从List传递的参数。但是,我只能使用此方法将参数传递到屏幕之一:
navigation.navigate('PostsTabNav', {
params: {
network: item,
},
screen: 'NetworkPosts' //or NetworkUsers
});
Run Code Online (Sandbox Code Playgroud)
我试图通过这样做直接将参数传递给我的导航器:
navigation.navigate('PostsTabNav', {
network: item
});
Run Code Online (Sandbox Code Playgroud)
第一个选项只允许我传递到一个屏幕。第二个选项允许我像这样访问导航器中的参数:
const PostsTabNav = createMaterialTopTabNavigator();
const PostsMainNav = (props) => {
const temp = props.route.params.network; //params here
return (
<PostsTabNav.Navigator>
<PostsTabNav.Screen name="NetworkPosts" component={NetworkPostsScreen} />
<PostsTabNav.Screen name="NetworkUsers" component={NetworkUsersScreen} />
</PostsTabNav.Navigator>
);
};
Run Code Online (Sandbox Code Playgroud)
有没有办法传递temp到我的两个屏幕?如果没有,有没有更好的方法来处理这种情况?
这是代码 StackNavigator
const NetworkListStackNav = createStackNavigator();
export const NetworksListNavigator = () => {
return ( …Run Code Online (Sandbox Code Playgroud) reactjs react-native react-navigation react-navigation-bottom-tab react-navigation-v5
我想在按下 BottomTabNavigator 上的特定选项卡时导航到屏幕。
通常,当按下该选项卡时,它会自动导航到配置的屏幕。但我不想有这种行为。我想隐藏该屏幕上的底部选项卡,并在顶部栏中提供返回功能。navigation.navigate('routeName')我通常在 ReactNavigationStack 屏幕中使用。但我不知道如何/在哪里在 BottomTabNavigator 配置中编写此代码。
例如,我在底部栏中有以下 5 个选项卡。我想在按下“添加”按钮时导航到“AddNewScreen” 。我不知道将 onPress 事件放在哪里。我试着把它放在和下。但仍然没有运气。optionsBottomTab.Screen
我试图拦截onPress事件来使用navigation.navigate。但它甚至没有被点击,它总是用标签栏打开 AddNewScreen。
<BottomTab.Navigator initialRouteName={INITIAL_ROUTE_NAME}>
<BottomTab.Screen
name="Home"
component={HomeScreen}
initialParams="Home Params"
options={{
title: 'Home',
tabBarIcon: ({ focused }) => <TabBarIcon focused={focused} name="md-home" iconType="ion" />,
}}
/>
<BottomTab.Screen
name="AddNew"
component={AddNewScreen}
options={{
title: 'Add',
tabBarIcon: ({ focused }) => <TabBarIcon focused={focused} name="md-add-circle" iconType="ion"
onPress={(e) => {
e.preventDefault();
console.log(e)
}} />,
}}
/>
</BottomTab.Navigator>
Run Code Online (Sandbox Code Playgroud)
添加新屏幕始终通过底部选项卡栏打开。
问题:
我有 3 个选项卡,每个选项卡都包含一组堆栈导航器。
const HomeNavigator = createStackNavigator();
const HomeStackNavigator = ({navigation, route}) => {
return (
<HomeNavigator.Navigator>
<HomeNavigator.Screen
name="Home"
component={Home}
/>
<HomeNavigator.Screen
name="Profile"
component={Profile}
/>
<HomeNavigator.Screen
name="Settings"
component={Settings}
/>
</HomeNavigator.Navigator>
);
};
Run Code Online (Sandbox Code Playgroud)
const StoreNavigator = createStackNavigator();
const StoreStackNavigator = ({navigation, route}) => {
return (
<StoreNavigator.Navigator>
<StoreNavigator.Screen
name="OurStore"
component={Store}
/>
</StoreNavigator.Navigator>
);
};
Run Code Online (Sandbox Code Playgroud)
const CommunityNavigator = createStackNavigator();
const CommunityStackNavigator = ({navigation, route}) => {
return (
<CommunityNavigator.Navigator>
<CommunityNavigator.Screen
name="Community"
component={Community}
/>
<CommunityNavigator.Screen
name="CommunityReply"
component={CommunityReply}
options={communityReplyOptions}
/>
<CommunityNavigator.Screen …Run Code Online (Sandbox Code Playgroud) react-native react-navigation react-navigation-stack react-navigation-bottom-tab
如代码所示,没有调用 tabPress,是我做错了还是我遗漏了什么,不幸的是我没有找到任何反应导航版本 5 的代码示例。
<Tab.Navigator labeled={false} barStyle={{backgroundColor: '#ffffff', height: 55}} options={{
tabPress: ({navigation}) => {
console.log('nav tab press triggered')
}
}}>
<Tab.Screen name={`DeviceNavigatorTab`} component={DeviceNavigator} options={{
tabBarIcon: ({tintColor}) => <Image source={require('../../images/feather_home-menu.png')}
style={{width: 26, height: 26, tintColor}}/>,
tabPress: ({navigation}) => {
console.log('tab press triggered')
}
}} tabPress={() => { console.log('prop tab pressed') }}/>
<Tab.Screen name={`AlarmNavigatorTab`} component={AlarmNavigator} options={{
tabBarIcon: ({tintColor}) => <Image source={require('../../images/feather_alert-circle.png')}
style={{width: 26, height: 26, tintColor}}/>,
}}/>
<Tab.Screen name={`ProfileNavigatorTab`} component={ProfileNavigator} options={{
tabBarIcon: ({tintColor}) => <Image source={require('../../images/feather_user.png')}
style={{width: 26, height: 26, tintColor}}/>, …Run Code Online (Sandbox Code Playgroud) react-native react-navigation-bottom-tab react-navigation-v5
正如您在下面看到的,我尝试了多种将背景颜色设置为绿色的方法,但都无济于事。背景像图像一样保持蓝色。
在inactiveColor和activeColor正在(白色,分别为红色)。
<NavigationContainer>
<Tab.Navigator
initialRouteName="HomeScreen"
activeColor="red"
inactiveColor="white"
activeBackgroundColor="green"
inactiveBackgroundColor="green"
style={{ backgroundColor: 'green' }}
tabBarOptions={{
style:{
backgroundColor: 'green'
}
}}
>
<Tab.Screen
name="HomeScreen"
options={{
tabBarLabel: 'Home',
tabBarIcon: ({ color }) => (
<MaterialCommunityIcons name="home" color={color} size={26} />
),
}}
>
{props => <HomeScreen {...props} state={this.state} />}
</Tab.Screen>
<Tab.Screen
name="Files"
component={FilesScreen}
options={{
tabBarLabel: 'Files',
tabBarIcon: ({ color }) => (
<MaterialCommunityIcons name="file" color={color} size={26} />
),
}}
/>
</Tab.Navigator>
</NavigationContainer>
Run Code Online (Sandbox Code Playgroud)
包.json
"dependencies": {
"@react-native-community/masked-view": "^0.1.7",
"@react-navigation/material-bottom-tabs": "^5.1.7",
"@react-navigation/native": …Run Code Online (Sandbox Code Playgroud) javascript reactjs react-native react-navigation react-navigation-bottom-tab
我正在使用createMaterialBottomTabNavigator并且我想将参数传递给路由:
return (
<Tab.Navigator initialRouteName="Home">
<Tab.Screen name="Home" component={home_customers} />
<Tab.Screen name="Favorites" component={favorite_vendors} />
<Tab.Screen name="More" component={settings_screen} />
</Tab.Navigator>
);
Run Code Online (Sandbox Code Playgroud)
我只能找到一个名为initialParams该属性的属性,我不确定在这种情况下使用它是否正确。所以,我想做的是从上一个屏幕获取参数并将该参数传递给这三个屏幕中的每一个!
更新
好的,我找到了如何获取参数,但我仍然不知道如何将这些参数传递到路由屏幕!
export default function home_customers_bar({ route, navigation }) {
const current_city_ = route.params.current_user_city_; //here I get the parameter from the previous screen
return (
<Tab.Navigator initialRouteName="Home">
<Tab.Screen name="Home" component={home_customers} />
<Tab.Screen name="Favorites" component={favorite_vendors} />
<Tab.Screen name="More" component={settings_screen} />
</Tab.Navigator>
);
}
Run Code Online (Sandbox Code Playgroud) react-native react-navigation react-navigation-bottom-tab react-navigation-v5
在我的 app.json 文件夹中,我将所有内容都很好地捆绑在一起,但在安装列表的底部有一个未定义的包:
“未定义”:“反应导航/底部选项卡”
我尝试运行"npm install react-navigation/bottom-tabs"但它引发了此错误:
warn 未定义的包已被忽略,因为它包含无效的配置。原因:找不到模块“undefined/package.json”
以前有人遇到过这样的问题吗?我正在运行React-Native版本 5 Expo。
编辑:事实证明它应该是@react-navigation/bottom-tabs,所以我安装了它并进行了调整;但现在我仍然坚持 app.json 文件夹中原始未定义的包,并且无法摆脱它。
json react-native react-navigation expo react-navigation-bottom-tab
我需要为活动选项卡添加一个指示器,我尝试添加 borderBottom,tabStyle但我们无法检查焦点。
使用react-navigation v5和createBottomTabNavigator作为底部选项卡。
这是我的代码:
<BottomTab.Navigator
tabBarOptions={{
activeTintColor: colors.brown,
labelPosition: 'below-icon',
}}>
<BottomTab.Screen
name="Home"
component={HomeTabNav}
options={{
tabBarLabel: 'Home',
tabBarIcon: ({focused}) => {
return focused ? (
<HomeSelectedIcon height={ms(24)} width={ms(24)} />
) : (
<HomeIcon height={ms(24)} width={ms(24)} />
);
},
}}
/>
...
</BottomTab.Navigator>
);
};
Run Code Online (Sandbox Code Playgroud)
提前致谢!
react-native ×10
react-navigation-bottom-tab ×10
reactjs ×2
android ×1
expo ×1
javascript ×1
json ×1