React Navigation: Bottom Tabs style - reactjs

How to apply a style on the bottom tab so that it looks similar to this model?
Image
<HomeTabs.Navigator
screenOptions={({route})=>({
tabBarIcon: ({color, size})=>{
const {name} = icons[route.name]
return <Ionicons name={name} size={size} color={color}/>
}
})}
tabBarOptions={
{
style: {
height: 50,
width: 300,
flexDirection: 'column',
alignSelf: 'center',
elevation: 2,
borderTopStartRadius: 5,
borderTopEndRadius: 5,
},
activeTintColor: '#845EC2',
}
}
>
result:
Result
There is no such container

tabBarStyle worked for me.
Refer official documentation for more styling options click here.
<Tab.Navigator
screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {
let iconName;
if (route.name === 'Home') {
iconName = focused? 'ios-home-sharp': 'ios-home-outline';
} else if (route.name === 'Favourites') {
iconName = focused ? 'ios-heart-sharp' : 'ios-heart-outline';
}
// You can return any component that you like here!
return <Ionicons name={iconName} size={size} color={color} />;
},
tabBarActiveTintColor: '#58ceb2',
tabBarInactiveTintColor: 'gray',
//Tab bar styles can be added here
tabBarStyle:{paddingVertical: 5,borderTopLeftRadius:15,borderTopRightRadius:15,backgroundColor:'white',position:'absolute',height:50},
tabBarLabelStyle:{paddingBottom:3},
})}
>
<Tab.Screen name="Home" component={HomeStackScreen} options={{headerShown: false}}/>
<Tab.Screen name="Favourites" component={FavouritesScreen} />
</Tab.Navigator>

This is what I came up with since there is no code. Hope this can help.
const Tab = createMaterialBottomTabNavigator();
const Navigator = () => {
return (
<NavigationContainer>
<Tab.Navigator
initialRouteName="Something"
barStyle={{ marginLeft:10, marginRight:10 }} //This is where you can manipulate its look.
>
<Tab.Screen name="firstOne" component={Something1}/>
<Tab.Screen name="secondOne" component={Something2}/>
<Tab.Screen name="thirdOne" component={Something3}/>
</Tab.Navigator>
</NavigationContainer>
);
}
Note the barStyle prop. That's where you can change how the bottom bar displays.

try this
add tabbaroptions
example
<tab.navigation tabbaroptions={{barstyle : styles.container}} >

I tried this and it works!
const Navigator = () => (
<Tab.Navigator tabBarOptions={{style:{backgroundColor: 'yellow'}}}> //do styling here
<Tab.Screen/>
<Tab.Screen/>
<Tab.Screen/>
</Tab.Navigator>
)
export default Navigator;

I got it to work with:
const screenOptionStyle = {
headerStyle: {
backgroundColor: Colors.darkGrey,
},
headerTintColor: Colors.richGold,
headerBackTitle: "",
tabBarStyle: [{ backgroundColor: Colors.darkGrey }], <--- HERE
};
<Tab.Navigator screenOptions={screenOptionStyle}>

Related

React Navigation change backgroundColor below tabBar

I am using React Navigations tabBar with my React Native project, and I don't know how to change the background color of my bottom tab bar properly. I used Expo to make my app and I have also edited app.json to have the correct backgroundColor, yet nothing changes. Here is my code:
function MyTabs() {
return (
<Tab.Navigator
initialRouteName="Feed"
screenOptions={{
tabBarActiveTintColor: "#E40066",
tabBarInactiveTintColor: "#fff",
tabBarActiveBackgroundColor: "#171717",
tabBarInactiveBackgroundColor: "#171717",
headerShown: false,
tabBarStyle: {
borderWidth: 1,
},
style: {
backgroundColor: "#171717",
},
}}
>
<Tab.Screen
name="Home"
component={Home}
options={{
tabBarLabel: "Home",
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons
name="glass-cocktail"
color={color}
size={size}
/>
),
}}
/>
<Tab.Screen
name="Search"
component={Search}
options={{
tabBarLabel: "Search",
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons name="magnify" color={color} size={size} />
),
}}
/>
<Tab.Screen
name="Saved"
component={Saved}
options={{
tabBarLabel: "Saved",
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons name="heart" color={color} size={size} />
),
}}
/>
</Tab.Navigator>
);
}
export default function App() {
const navTheme = {
...DefaultTheme,
colors: {
...DefaultTheme.colors,
background: "#171717",
},
};
return (
<NavigationContainer theme={navTheme}>
<MyTabs style={{ backgroundColor: "#171717" }}></MyTabs>
</NavigationContainer>
);
}
Yet my tabBar looks like this, I want it to be #171717, not white... Thank you in advance
The solution was to make a theme to change the background color:
export default function App() {
const navTheme = {
colors: {
background: "#171717"
}
};
return (
<NavigationContainer theme={navTheme}>
<MyTabs style={{ backgroundColor: "#171717" }}></MyTabs>
</NavigationContainer>
);
}
You can use this
<Tab.Navigator
tabBarOptions={{
style: {
backgroundColor:'#171717 '
}
}}>
{Screens}
</Tab.Navigator>

How to prevent re rendering in react-native material-top-tabs-navigatior?

I'm using material top tabs navigatior
https://reactnavigation.org/docs/material-top-tab-navigator
When i switching tabs, the components are always re-rendered.
How can i prevent re-rendering??
here is my code
please help me!!!!
const Menu = () => {
return (...)
}
const Info = () => {
return (...)
}
const Review = () => {
return (...)
}
<Tab.Navigator
screenOptions={{
tabBarScrollEnabled: false,
tabBarStyle: { backgroundColor: '#FFFFFF' },
tabBarPressOpacity: true
}}
style={styles.tabBar}
>
<Tab.Screen
name="Menu"
component={Menu}
options={{
tabBarShowLabel: false,
tabBarIcon: ({ forcused, color }) => {
return <Text style={styles.tabBarText}>메뉴</Text>;
},
}}
/>
<Tab.Screen
name="Info"
component={Info}
options={{
tabBarShowLabel: false,
tabBarIcon: ({ forcused, color }) => {
return <Text style={styles.tabBarText}>정보</Text>;
},
}}
/>
<Tab.Screen
name="Review"
component={Review}
options={{
tabBarShowLabel: false,
tabBarIcon: ({ forcused, color }) => {
return <Text style={styles.tabBarText}>리뷰</Text>;
},
}}
/>
</Tab.Navigator>
This post is a bit old, however for those who face the same issue you can try to add the lazy option to the navigator (or the screen).
const Tab = createMaterialTopTabNavigator();
<Tab.Navigator
screenOptions={{
tabBarScrollEnabled: false,
tabBarStyle: { backgroundColor: '#FFFFFF' },
tabBarPressOpacity: true,
lazy: true // add the option here
}}
style={styles.tabBar}
>
...
Restart the server and the screen now only render as it comes to the viewPort.
You can also use the lazyPreloadDistance when lazy is enabled.
Here is the official doc

is there any method to re-render the screen to bottom tab nav v5 in react-native

Hy, I'm using react-native bottom tabs navigation the first time I'm having an issue to re-render the screen when I get back to some other tab. I try to use isFocused in useEffect() but it didn't work for me.
const LandScreen = ({navigation}) => {
return (
<Tab.Navigator
tabBarOptions={{
showLabel: false,
activeTintColor: colors.primary,
inactiveTintColor: colors.textHead,
}}
>
<Tab.Screen
name="Home"
component={HomeScreen}
options={{
tabBarIcon: ({focused, color, size}) => {
const icon = focused ? 'home' : 'home';
return <Icon name={icon} color={color} size={size} />;
},
}}
/>
<Tab.Screen
name="Message"
component={ChatStackScreen}
options={{
tabBarIcon: ({focused, color, size}) => {
const icon = focused ? 'chatbox-ellipses' : 'chatbox-ellipses';
return <Icon name={icon} color={color} size={size} />;
},
}}
/>
<Tab.Screen
name="Requests"
component={RequestStackScreen}
options={{
tabBarIcon: ({focused, color, size}) => {
const icon = focused ? 'heart' : 'heart';
return <Icon name={icon} color={color} size={size} />;
},
}}
/>
<Tab.Screen
name="Notifications"
component={NotificationStackScreen}
options={{
tabBarIcon: ({focused, color, size}) => {
const icon = focused ? 'notifications' : 'notifications';
return <Icon name={icon} color={color} size={size} />;
},
}}
/>
<Tab.Screen
name="Profile"
component={ProfileScreenScreen}
options={{
tabBarIcon: ({focused, color, size}) => {
const icon = focused ? 'person' : 'person';
return <Icon name={icon} color={color} size={size} />;
},
}}
/>
</Tab.Navigator>
);
};
Each Tab component having a stack inside of it.
Someone informs me how to rerender the bottom navigation tabs?
If I understand you correctly, you want the screen to re-render when you click on a bottom tab navigation button.
This can be accomplished by either adding the 'focus' event listener via the react navigation docs https://reactnavigation.org/docs/function-after-focusing-screen/
Or by explicitly calling 'navigation.navigate' when a bottom tab is pressed.
Assuming I understand your question, all you want to do is re-render the icons when the tab changes?
React Navigation has docs & an example for this use-case for tab-based navigation here.
Posting the code sample from the docs here for ease-of-access:
import Ionicons from 'react-native-vector-icons/Ionicons';
// (...)
export default function App() {
return (
<NavigationContainer>
<Tab.Navigator
screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {
let iconName;
if (route.name === 'Home') {
iconName = focused
? 'ios-information-circle'
: 'ios-information-circle-outline';
} else if (route.name === 'Settings') {
iconName = focused ? 'ios-list-box' : 'ios-list';
}
// You can return any component that you like here!
return <Ionicons name={iconName} size={size} color={color} />;
},
tabBarActiveTintColor: 'tomato',
tabBarInactiveTintColor: 'gray',
})}
>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen name="Settings" component={SettingsScreen} />
</Tab.Navigator>
</NavigationContainer>
);
}
If your question meant re-rendering the screen when the tab changes:
Leveraging useEffect with useIsFocused should work when changing tabs. You haven't included your example with those hooks, so I'm not exactly sure how you're using it.
The following snippet should get you up and running with said hooks. In this case, you can run whatever you want inside the useEffect when switching tabs, where the active tab gets triggered.
If for some reason you just want the UI to get updated/persisted based on some data between tab changes you might want to consider using Redux.
import React, { useEffect } from "react";
import { Text, View } from "react-native";
import { NavigationContainer } from "#react-navigation/native";
import { createBottomTabNavigator } from "#react-navigation/bottom-tabs";
import { useIsFocused } from "#react-navigation/core";
function HomeScreen() {
const isFocused = useIsFocused();
useEffect(() => {
if (isFocused) {
console.log("HELLO HOME");
}
}, [isFocused]);
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Home!</Text>
</View>
);
}
function SettingsScreen() {
const isFocused = useIsFocused();
useEffect(() => {
if (isFocused) {
console.log("HELLO SETTINGS");
}
}, [isFocused]);
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Settings!</Text>
</View>
);
}
const Tab = createBottomTabNavigator();
export default function App() {
return (
<NavigationContainer>
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen name="Settings" component={SettingsScreen} />
</Tab.Navigator>
</NavigationContainer>
);
}

React Navigation 5 createMaterialTopTabNavigator Disable Swipe depending on screen [duplicate]

Wanna make only Map component swipe-disabled but the entire screens were applied when using "swipeEnabled".
How can I do?
const Tab = createMaterialTopTabNavigator();
const Tabs = () => {
return (
<Tab.Navigator
swipeEnabled={false} // <- Screens can be swiped but it is applied to every screen.
{...}
>
<Tab.Screen
name="Home"
component={Home}
/>
<Tab.Screen
name="Map"
component={Map}
/>
</Tab.Navigator>
);
}
const App = () => {
return (
<NavigationContainer>
<SafeAreaView style={styles.safeAreaView} />
<Tabs />
</NavigationContainer>
);
}
You could pass a state value to swipeEnabled and update the value to false if you're on the Map screen like this:
const Tab = createMaterialTopTabNavigator();
const Tabs = () => {
const [swipeEnabled, setSwipeEnabled] = useState(true);
return (
<NavigationContainer>
<Tab.Navigator
swipeEnabled={swipeEnabled}
screenOptions={({ navigation, route }) => {
if (route.name === 'Map' && navigation.isFocused()) {
setSwipeEnabled(false);
} else if (route.name !== 'Map' && navigation.isFocused()) {
setSwipeEnabled(true);
}
}}>
<Tab.Screen name="Home" component={Home} />
<Tab.Screen name="Map" component={Map} />
</Tab.Navigator>
</NavigationContainer>
);
};
As swipeEnabled={swipeEnable} is depreciated you must use this is screenOption in Tab.Navigator
screenOption comes with various options one of them is swipeEnabled
const Tab = createMaterialTopTabNavigator();
const Tabs = () => {
return (
<NavigationContainer>
<Tab.Navigator
screenOptions={{
swipeEnabled: false,
tabBarStyle: {
backgroundColor: 'transparent',
elevation: 0,
shadowOpacity: 0,
borderBottomWidth: 0,
},
tabBarLabelStyle: {
fontSize: 12,
textTransform: 'capitalize',
},
tabBarActiveTintColor: '#000',
tabBarInactiveTintColor: '#000',
tabBarIndicatorStyle: {
backgroundColor: 'transparent',
}
}}>
<Tab.Screen name="Home" component={Home} />
<Tab.Screen name="Map" component={Map} />
</Tab.Navigator>
</NavigationContainer>
);
};

React Native: Change color of navigation tab

On my navigation tab, I want to change the color of the label + the color of the icon when its active, so what I did is created an if and else statement:
<MealsFavTabNavigator.Screen
name="Favorites"
component={FavoritesScreen}
options={({ route }) => ({
tabBarIcon: (tabInfo) => {
if (route.name === 'Favorites'){
return <Ionicons name="ios-star" size={25} color='tomato'/>
} else {
return <Ionicons name="ios-star" size={25} color='black' />
}
}
})}
My label color is also fixed above the Navigator level:
<MealsFavTabNavigator.Navigator
tabBarOptions={{
activeTintColor: Colors.primaryColor,
inactiveTintColor: 'black',
}}>
Here's my full code:
import React from 'react';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import { Ionicons } from '#expo/vector-icons';
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 (
<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>
);
};
const MealsFavTabNavigator = createBottomTabNavigator();
const MealsTabNav = () => {
return (
<NavigationContainer>
<MealsFavTabNavigator.Navigator
tabBarOptions={{
activeTintColor: Colors.primaryColor,
inactiveTintColor: 'black',
}}>
<MealsFavTabNavigator.Screen
name="Meals"
component={MealsNavigator}
options={({ route }) => ({
tabBarIcon: ({ color }) => {
if(route.name === 'Meals'){
color = 'tomato'
} else if (route.name === 'Favorites') {
color = 'black'
}
return <Ionicons name="ios-restaurant" size={25} color={color}/>
}
})}
/>
<MealsFavTabNavigator.Screen
name="Favorites"
component={FavoritesScreen}
options={({ route }) => ({
tabBarIcon: (tabInfo) => {
if (route.name === 'Favorites'){
return <Ionicons name="ios-star" size={25} color='tomato'/>
} else {
return <Ionicons name="ios-star" size={25} color='black' />
}
}
})}
/>
</MealsFavTabNavigator.Navigator>
</NavigationContainer>
);
};
export default MealsTabNav;
How can I change the color of the label and color of the iconicons when its active? My solution doesnt work.
By default there are parameters for colors in tabbar icon and tabbar label and these are set to the active tint color and inactive tint color.
But if you have a requirement to override this you can do the following
<Tab.Screen
name="Feed"
component={Feed}
options={{
tabBarLabel:({ focused,color })=>(<Text style={{color:focused?"red":"aqua"}}>1232</Text>),
tabBarIcon: ({ focused,color, size }) => (
<MaterialCommunityIcons name="home" color={focused?"green":"blue"} size={size} />
),
}}
/>
References on the props
tabBarLabel can be a text or a react node, and you get the focused and the color as arguments, The color will be the color you set as activetintcolor or inactivetintcolor.
focused is a boolean on whether the tab is focused or not.
Same arguments are passed to the tabBarIcon only difference is the size which is the size of the icon.
If you see the code above, I have given custom colors based on focused without using the color that is passed. You can do this as per your requirement.
https://stackoverflow.com/a/63033684/17735463 - This is a valid answer but mine works fine too (react-navigation-v5)
<Tab.Navigator
screenOptions={({ route }) => ({
tabBarIcon: ({ focused }) => {
let iconName;
let size=25;
let color = focused ? 'black' : 'gray';
if (route.name === 'HomeScreen') {
iconName = 'ios-home';
return <Ionicons name={iconName} size={size} color={color} />;
} else if (route.name === 'FavoritesScreen') {
iconName = 'star';
return <AntDesign name={iconName} size={size} color={color} />;
}
tabBarLabel: ({ focused }) => {
let titleStyle = {
fontSize: 12,
fontWeight: focused ? 'bold' : '500',
color: focused ? 'black' : 'gray',
};
if (route.name === 'HomeScreen') {
return <Text style={titleStyle}>Home</Text>;
} else if (route.name === 'FavoritesScreen') {
return <Text style={titleStyle}>Favorites</Text>;
}
},
})}>
<Tab.Screen name='Home' component={HomeScreen}/>
<Tab.Screen name='Favorites' component={FavoritesScreen}/>
</Tab.Navigator>

Resources