react native why my navigation shows me 2 headers? - react-native

I want to custom my header. The Problem is, it shows me two headers. My custom header and the Tab Header "Home".
import * as React from 'react';
import { StyleSheet, Text, View, TouchableOpacity, Image } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import {
BottomTabBar,
createBottomTabNavigator,
} from '#react-navigation/bottom-tabs';
import { Entypo, FontAwesome5 } from '#expo/vector-icons';
import Home from '../screens/Home';
import News from '../screens/News';
import Weapons from '../screens/stack/Weapons';
import HeaderHome from '../shared/headerHome';
const Stack = createNativeStackNavigator();
const Tab = createBottomTabNavigator();
const TabBar = () => (
<Tab.Navigator
tabBar={(props) => {
return (
<View>
<BottomTabBar {...props} />
</View>
);
}}
screenOptions={{
tabBarStyle: [{ display: 'flex' }],
tabBarShowLabel: true,
tabBarLabelStyle: {
paddingBottom: 6,
color: '#333',
},
}}>
<Tab.Screen
name="Home"
component={Home}
options={{
tabBarIcon: ({ color, focused }) => (
<Entypo name="home" size={24} color={focused ? '#37bfff' : '#888'} />
),
}}
/>
<Tab.Screen
name="News"
component={News}
options={{
tabBarIcon: ({ color, focused }) => (
<Entypo
name="newsletter"
size={24}
color={focused ? '#37bfff' : '#888'}
/>
),
}}
/>
</Tab.Navigator>
);
const Navigation = () => (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="HomeScreen"
component={TabBar}
options={{
header: () => <HeaderHome />,
}}
/>
</Stack.Navigator>
</NavigationContainer>
);
const styles = StyleSheet.create({
navigator: {
borderTopWidth: 0,
backgroundColor: 'transparent',
elevation: 30,
height: 60,
},
});
export default Navigation;

You can add a option to hide the Tab.Navigator's header. Add headerShown: false to the screenOptions of Tab.Navigator as shown below
<Tab.Navigator
tabBar={(props) => {
return (
<View>
<BottomTabBar {...props} />
</View>
);
}}
screenOptions={{
tabBarStyle: [{ display: 'flex' }],
tabBarShowLabel: true,
tabBarLabelStyle: {
paddingBottom: 6,
color: '#333',
},
headerShown: false, // <--- Here
}}>
Check out the working example for your scenario here
On the other hand, if you want to hide the Stack.Navigator's header, you can do as shown below
<Stack.Navigator screenOptions={{ headerShown: false }}>

Related

Remove oval shape when active when using material bottom tabs

I'm using #react-navigation/material-bottom-tabs and have an issue with the tab icons in the bottom tab navigator. The problem is an oval shape, as the background shows up when that tab is selected.
This is what it looks like:
So far I didn't find any way to remove it.
import React, { Component } from "react";
import { StatusBar } from "expo-status-bar";
import { StyleSheet, Text, View } from "react-native";
import HomeScreen from "./Screens/Home";
import NotificationScreen from "./Screens/Notifications";
import ProfileScreen from "./Screens/Profile";
import SearchScreen from "./Screens/Search";
import { createMaterialBottomTabNavigator } from "#react-navigation/material-bottom-tabs";
import { NavigationContainer } from "#react-navigation/native";
import MaterialCommunityIcons from "react-native-vector-icons/MaterialCommunityIcons";
const Tab = createMaterialBottomTabNavigator();
export default function App() {
return (
<NavigationContainer>
<Tab.Navigator
shifting={true}
initialRouteName="Home"
activeColor="#fff"
inactiveColor="#333"
barStyle={{ backgroundColor: "tomato" }}
labeled={true}
>
<Tab.Screen
name="Home"
component={HomeScreen}
options={{
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons name="home" color={color} size={26} />
),
tabBarColor: "blue",
}}
/>
<Tab.Screen
name="Notifications"
component={NotificationScreen}
options={{
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons name="heart" color={color} size={26} />
),
}}
/>
<Tab.Screen
name="Profile"
component={ProfileScreen}
options={{
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons
name="account-circle"
color={color}
size={26}
/>
),
}}
/>
<Tab.Screen
name="Search"
component={SearchScreen}
options={{
tabBarIcon: ({ color }) => (
<MaterialCommunityIcons name="magnify" color={color} size={26} />
),
}}
/>
</Tab.Navigator>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
I have tried many ways to remove it, but I haven't found anything that works.

What is the proper way to have only one header with Open Drawer and Back button?

What I need is a single header with a back button on StackScreen2 and a toggle drawer button in StackScreen1.
I apologise for my poor writing and English.
import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { createDrawerNavigator } from '#react-navigation/drawer';
import { NavigationContainer } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
const Stack = createNativeStackNavigator();
function StackScreen1({ navigation }) {
return (
<View>
<Text>StackScreen1</Text>
<Button title="Go To StackScreen2" onPress={() => navigation.navigate("StackScreen2")} />
</View>
)
}
function StackScreen2() {
return (
<View>
<Text>StackScreen2</Text>
</View>
)
}
function DrawerScreen1() {
return (
<Stack.Navigator>
<Stack.Screen name="StackScreen1" component={StackScreen1} />
<Stack.Screen name="StackScreen2" component={StackScreen2} />
</Stack.Navigator>
)
}
function DrawerScreen2() {
return (
<View>
<Text>DrawerScreen2</Text>
</View>
)
}
const Drawer = createDrawerNavigator();
export default function Test3() {
return (
<NavigationContainer>
<Drawer.Navigator>
<Drawer.Screen name="DrawerScreen1" component={DrawerScreen1} />
<Drawer.Screen name="DrawerScreen2" component={DrawerScreen2} />
</Drawer.Navigator>
</NavigationContainer>
);
}
StackScreen1
StackScreen2
I tried hiding drawer header but then I don't have toggle drawer button anywhere.
Check below code for drawer navigation:
import * as React from 'react';
import { Button, View } from 'react-native';
import { createDrawerNavigator } from '#react-navigation/drawer';
import { NavigationContainer } from '#react-navigation/native';
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button
onPress={() => navigation.navigate('Notifications')}
title="Go to notifications"
/>
</View>
);
}
function NotificationsScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button onPress={() => navigation.goBack()} title="Go back home" />
</View>
);
}
const Drawer = createDrawerNavigator();
export default function App() {
return (
<NavigationContainer>
<Drawer.Navigator useLegacyImplementation initialRouteName="Home">
<Drawer.Screen name="Home" component={HomeScreen} />
<Drawer.Screen name="Notifications" component={NotificationsScreen} />
</Drawer.Navigator>
</NavigationContainer>
);
}
You can check more about drawer here.
May be this will help you!
import * as React from 'react';
import { Ionicons, FontAwesome } from '#expo/vector-icons';
import { Button, View, Text, Pressable,Image } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
import { createDrawerNavigator } from '#react-navigation/drawer';
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => navigation.navigate('SettingsScreen')}
/>
</View>
);
}
function DetailsScreen() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
<Button
title="Go to Details"
onPress={() => navigation.goBack()}
/>
</View>
);
}
const Drawer = createDrawerNavigator();
function MyDrawer() {
return (
<Drawer.Navigator
useLegacyImplementation
// screenOptions={{
// drawerLabelStyle: { fontSize: 17, color: '#000',marginLeft:-30 }
// }}
>
<Drawer.Screen
name="HomeScreen"
component={DetailsScreen}
screenOptions={{
headerShown: false,
}}
/>
</Drawer.Navigator>
);
}
function MyTabs() {
return (
<Tab.Navigator>
<Tab.Screen
name="SettingsScreen"
component={MyDrawer}
options={{
headerShown: false,
tabBarIcon: ({ color, size }) => (
<Ionicons name="md-home" size={24} color={'#eee'} />
),
tabBarStyle: { position: 'absolute' },
tabBarActiveTintColor: '#eee',
tabBarInactiveTintColor: '#eee',
}}
/>
<Tab.Screen
name="DetailsScreen"
component={DetailsScreen}
options={{
headerShown: false,
tabBarIcon: ({ color, size }) => (
<Image source={{uri:"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTUAN0zLcvLFjdaYN1dBRSKoIuJndRW6VuX85w2yzEzxvTwO3B0Y_gv1x8kVDISNKJEbq0&usqp=CAU"}}
style={{width:24,height:24,borderRadius:24}} />
),
tabBarStyle: { position: 'absolute' },
tabBarActiveTintColor: '#eee',
tabBarInactiveTintColor: '#eee',
}}
/>
</Tab.Navigator>
);
}
function SettingsScreen() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>2</Text>
</View>
);
}
const Stack = createNativeStackNavigator();
const Tab = createBottomTabNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="HomeScreen">
<Stack.Screen
name="HomeScreen"
component={HomeScreen}
options={{ headerShown: false }}
/>
<Stack.Screen
name="SettingsScreen"
component={MyTabs}
options={{ headerShown: false }}
/>
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;

React Native: how I add border bottom to selected icon in Tab Navigator?

I want to put a white border bottom in my tab bar, but it is not being accepted. I'm using the following: tabBarSelectedItemStyle: [{
borderBottomWidth: 2,
borderBottomColor: 'white',
}],
What would you recommend I do?
i'm not getting it in any way because I don't understand why it's not being accepted
enter image description here
Navigator.js
import { createBottomTabNavigator } from "#react-navigation/bottom-tabs";
import { createStackNavigator } from "#react-navigation/stack";
import { NavigationContainer } from '#react-navigation/native';
import { Entypo, Feather } from 'react-native-vector-icons';
import Settings from "../screens/Settings";
import Login from "../screens/Login";
import Tasks from "../screens/Tasks";
import Home from "../screens/Home";
import React from 'react'
const Tab = createBottomTabNavigator();
const MainTabNavigator = () => {
return (
<>
<Tab.Navigator
screenOptions={{
tabBarActiveTintColor: "#A0D800",
tabBarInactiveTintColor: "#FFFFFF",
backgroundColor: "#FFFFFF",
headerShown: false,
tabBarSelectedItemStyle: [{
borderBottomWidth: 2,
borderBottomColor: 'white',
}],
tabBarStyle: [
{
display: "flex",
backgroundColor: "#000000",
borderTopLeftRadius: 20,
borderTopRightRadius: 20,
height: 70,
overflow: 'hidden',
alignItems: 'center',
justifyContent: 'center',
},
null,
],
tabBarHideOnKeyboard: true,
tabBarLabelStyle: {
backgroundColor: "#000000",
width: 60,
flex: 0.000004,
},
}}
>
<Tab.Screen name="Tasks"
component={Tasks}
options={{
tabBarIcon: ({ color }) => (
<Entypo name="add-to-list"
size={30}
color={color}
/>
)
}}
/>
<Tab.Screen name="Home"
component={Home}
options={{
tabBarIcon: ({ color }) => (
<Feather name="home"
size={30}
color={color}
/>
)
}} />
<Tab.Screen name="Settings"
component={Settings}
options={{
tabBarIcon: ({ color }) => (
<Feather name="settings"
size={30}
color={color}
/>
)
}}
/>
</Tab.Navigator>
</>
)
}
const Stack = createStackNavigator();
const MainNavigator = () => {
return (
<>
<Stack.Navigator screenOptions={{ headerShown: false }}>
<Stack.Screen name="Login" component={Login} />
<Stack.Screen name="TabNavigator" component={MainTabNavigator} />
</Stack.Navigator>
</>
)
}
export default MainNavigator;

Hide Tab Bar on invoking keyboard for createMaterialTopTabNavigator as a Bottom Tab Navigator implementation

Heyy everyone, I was using the React Navigation 5 createMaterialTopTabNavigator for a Bottom Navigation setup as I needed the swipe transition that Material Top Navigator offers as well, but unlike createBottomTabNavigator, the Material Top doesn't have a keyboardHidesTabBar so that I can make sure that tab bar is hidden when the keyboard opens, but thats something I'm trying to achieve (https://material.io/components/bottom-navigation#placement).
Any other way to get something like this done?
Here's the current snack to play around: https://snack.expo.io/qTDqLo1eb
Here's the code:
import * as React from 'react';
import { Text, View, TextInput } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createMaterialTopTabNavigator } from '#react-navigation/material-top-tabs';
import { MaterialCommunityIcons } from '#expo/vector-icons';
function HomeScreen() {
const [text, onChangeText] = React.useState("Useless Text");
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home!</Text>
<TextInput
onChangeText={onChangeText}
value={text}
/>
</View>
);
}
function SettingsScreen() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Settings!</Text>
</View>
);
}
const Tab = createMaterialTopTabNavigator();
export default function App() {
return (
<NavigationContainer>
<Tab.Navigator tabBarPosition='bottom'
tabBarOptions={{
// style: { position: 'absolute', bottom:0 },
activeTintColor: '#e91e63',
inactiveTintColor: '#ee11ff',
showIcon: true,
indicatorStyle:{
height:0
}
}}>
<Tab.Screen
options={{
tabBarLabel: 'Home',
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons name="home" color={color} size={24} />
),
}} name="Home" component={HomeScreen} />
<Tab.Screen
options={{
tabBarLabel: 'Profile',
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons name="account" color={color} size={24} />
),
}} name="Settings" component={SettingsScreen} />
</Tab.Navigator>
</NavigationContainer>
);
}

React Native drawer navigation screen header title and buttons

I am trying to add title and buttons to my home screen that is opened from drawer navigation. The following does not work(screen loads but no header) and i cant find documentation or sample on this that uses React Navigation v5. Did i miss something? This is the doc i am following.
<Drawer.Screen
name="Home"
component={HomeScreen}
title="Home Screen"
options={{
headerRight: () => (
<Button
onPress={() => alert("This is a button!")}
title="Info"
color="#fff"
/>
),
}}
/>
UPDATE: doing this on the screen didn't work as well.
const HomeScreen = (props) => {
return (
<Container>
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<Text>Home Screen2</Text>
</View>
</Container>
);
};
HomeScreen.navigationOptions = (props) => ({
title: "Home",
headerStyle: {
backgroundColor: "rgb(0, 145, 234)",
},
headerTintColor: "white",
headerTitleStyle: {
fontWeight: "bold",
color: "white",
},
});
export default HomeScreen;
From the option you put in, I guess you may want to add a Stack inside your Drawer navigation.
From the below example, when you go to the HomeStack, it will have a navigation bar and you can custom it using the option you put in.
(I also created a simple snack here: https://snack.expo.io/#gie3d/insane-ice-cream)
import React from 'react';
import { Text, View } from 'react-native';
import { NavigationContainer, DrawerActions } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import { createDrawerNavigator } from '#react-navigation/drawer';
import { Ionicons } from '#expo/vector-icons';
const Stack = createStackNavigator();
const Drawer = createDrawerNavigator();
const HomeStack = () => (
<Stack.Navigator>
<Stack.Screen name="Home" component={Home} options={({navigation}) => ({
title: "Home",
headerStyle: {
backgroundColor: "rgb(0, 145, 234)",
},
headerTintColor: "white",
headerTitleStyle: {
fontWeight: "bold",
color: "white",
},
headerLeft: () => (
<Ionicons
name={'md-menu'}
size={24}
style={{ marginLeft: 10 }}
onPress={() =>
navigation.dispatch(DrawerActions.toggleDrawer())
}
/>
),
})} />
</Stack.Navigator>
);
const Home = () => {
return (
<View>
<Text>This is Home</Text>
</View>
)}
export default () => {
return (
<NavigationContainer>
<Drawer.Navigator initialRouteName="HomeStack">
<Drawer.Screen name="HomeStack" component={HomeStack} />
<Drawer.Screen name="HomeNoStack" component={Home} />
</Drawer.Navigator>
</NavigationContainer>
);
}
You need to create the button on your screen too, like this.
import React from "react";
import {Text, View} from 'react-native';
export default class News extends React.Component {
static navigationOptions = ({navigation}) => {
return {
//headerLeft: --- PUT HERE YOU CUSTOM BUTTON (Use navigation.goBack() in onPress)
headerLeft:(<HeaderBackButton size={20} onPress={()=>{navigation.navigate('Home')}} color="#fff" tintColor="#fff"/>),
headerStyle: {
backgroundColor: 'rgb(0, 145, 234)',
},
headerTintColor: 'white',
headerTitleStyle:
{
fontWeight: 'bold',
color:'white' ,
//paddingTop:"2%",
},
}
}
render() {
return (
<View>
<Text> Here Leave the News!! </Text>
</View>
);
}
}