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

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>
);
};

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 shorten the code of adding header in drawer Navigator?

I am new to react native, I am using react navigator
Now I am trying to add the header to the pages. But I think my code is not following the dry principle.
here is the pic
Here is my code
const Drawer = createDrawerNavigator();
const HomeStack = createStackNavigator();
const AboutStack = createStackNavigator();
//this one
const HomeStackScreen = ({navigation}) => (
<HomeStack.Navigator
screenOptions={{
headerStyle: {backgroundColor: '#009388'},
headerTintColor: '#fff',
}}>
<HomeStack.Screen
name="Home"
component={HomeScreen}
options={{
title: 'Home',
headerLeft: () => (
<Icon.Button
name="bike"
size={25}
backgroundColor="#009388"
onPress={() => {
navigation.openDrawer();
}}></Icon.Button>
),
}}
/>
</HomeStack.Navigator>
);
//this one
const AboutStackScreen = ({navigation}) => (
<AboutStack.Navigator
screenOptions={{
headerStyle: {backgroundColor: '#009388'},
headerTintColor: '#fff',
}}>
<AboutStack.Screen
name="About"
component={AboutScreen}
options={{
title: 'About',
headerLeft: () => (
<Icon.Button
name="bike"
size={25}
backgroundColor="#009388"
onPress={() => {
navigation.openDrawer();
}}></Icon.Button>
),
}}
/>
</AboutStack.Navigator>
);
const DrawerNavigator = ({navigation}) => {
return (
<Drawer.Navigator drawerContent={(props) => <DrawerContent {...props} />}>
<Drawer.Screen name="Home" component={HomeStackScreen} />
<Drawer.Screen name="More" component={MoreScreen} />
<Drawer.Screen name="About" component={AboutStackScreen} />
<Drawer.Screen name="FindBike" component={FindBikeShop} />
{/* Aricles Screens */}
<Drawer.Screen name="Cases" component={Cases} />
<Drawer.Screen name="Guides" component={Guides} />
<Drawer.Screen name="LatestBlogs" component={LatestBlogs} />
<Drawer.Screen name="LatestNews" component={LatestNews} />
<Drawer.Screen name="News" component={News} />
);
};
As you can see in the code. I am rewriting same createStackNavigator code for HomeStack & AboutStack. As the page grow , my code base will also grow larger. Is there anyway to shortern these codes?
You can create function to return the base navigator you find yourself reusing and pass any unique values as arguments.
const createMyStackNavigator = (name, screen) => ({ navigation }) => {
const Stack = createStackNavigator();
return (
<Stack.Navigator
screenOptions={{
headerStyle: { backgroundColor: '#009388' },
headerTintColor: '#fff',
}}
>
<Stack.Screen
name={name}
component={screen}
options={{
title: name,
headerLeft: () => (
<Icon.Button
name="bike"
size={25}
backgroundColor="#009388"
onPress={() => {
navigation.openDrawer();
}}
></Icon.Button>
),
}}
/>
</Stack.Navigator>
);
};
const HomeStackScreen = createMyStackNavigator('Home', HomeScreen);
const AboutStackScreen = createMyStackNavigator('About', AboutScreen);

React Native Navigation Error: Warning: Can't perform a React state update on an unmounted component

I am working on React native project, after sign in, when it jumps from signIn screen to Home screen then I am getting this Error warning, I have tried all solutions, like component unmount in useEffect, but still getting the same error. This is my App js component, where I have defined all routes.
So please suggest me a good solution, I Am also getting the same issue on some other screens especially when I navigate to the previous screen
const AuthStack = createStackNavigator();
export const AuthStackScreen = () => (
<AuthStack.Navigator>
<AuthStack.Screen name="Signin" component={Signin} />
<AuthStack.Screen name="Signup" component={Signup} />
</AuthStack.Navigator>
);
const HomeStack = createStackNavigator();
export const HomeStackScreen = () => (
<HomeStack.Navigator
screenOptions={({ route, navigation }) => ({
headerShown: false,
gestureEnabled: true,
cardOverlayEnabled: true,
headerStatusBarHeight:
navigation
.dangerouslyGetState()
.routes.findIndex((r) => r.key === route.key) > 0
? 0
: undefined,
// ...TransitionPresets.ModalSlideFromBottomIOS,
})}
mode="modal">
<HomeStack.Screen name="Home" component={Home} unmountOnBlur={true}
options={{ ...TransitionPresets.ModalTransition, unmountOnBlur: true }} />
<HomeStack.Screen name="Hotels" component={Hotels} unmountOnBlur={true}
options={{ ...TransitionPresets.ModalTransition, unmountOnBlur: true }} />
<HomeStack.Screen name="Rooms" component={Rooms} options={{
...TransitionPresets.ModalTransition }} />
<HomeStack.Screen name="ReserveRoom" component={ReserveRoom} options={{
...TransitionPresets.ModalTransition }} />
<HomeStack.Screen name="News" component={News} options={{
...TransitionPresets.ModalTransition }} />
<HomeStack.Screen name="Deals" component={Deals} options={{
...TransitionPresets.ModalTransition }} />
<HomeStack.Screen name="Destinations" component={Destinations} options={{
...TransitionPresets.ModalTransition }} />
<HomeStack.Screen name="Signin" component={Signin} options={{
...TransitionPresets.ModalTransition }} />
<HomeStack.Screen name="Signup" component={Signup} options={{
...TransitionPresets.ModalTransition }} />
</HomeStack.Navigator>
);
const ProfileStack = createStackNavigator();
export const ProfileStackScreen = () => (
<ProfileStack.Navigator>
<ProfileStack.Screen name="Profile" component={Profile} />
</ProfileStack.Navigator>
);
const Drawer = createDrawerNavigator();
const DrawerScreen = () => (
<Drawer.Navigator initialRouteName="Home" drawerContent={props => <CustomDrawerContent
{...props} />}>
<Drawer.Screen name="Home" unmountOnBlur={true}
options={{ unmountOnBlur: true }} component={TabsScreen} />
<Drawer.Screen name="Profile" component={ProfileStackScreen} />
</Drawer.Navigator>
);
const RootStack = createStackNavigator();
const RootStackScreen = () => (
<RootStack.Navigator headerMode="none">
<RootStack.Screen name="App" component={DrawerScreen} options={{ animationEnabled: false }} />
</RootStack.Navigator>
);
const AuthDStack = createStackNavigator();
const AuthDStackScreen = () => (
<AuthDStack.Navigator headerMode="none">
<AuthDStack.Screen name="Home" component={TabsScreen} />
</AuthDStack.Navigator>
);
export default function App() {
// It convert Auth Headers in correct format
if (!global.btoa) {
global.btoa = encode;
}
if (!global.atob) {
global.atob = decode;
}
LogBox.ignoreAllLogs(true)
const dispatch = useDispatch();
const { user, isLoading, error, csrfToken, userDetail } = useSelector((state) => {
return {
userDetail: state.Auth.userDetail,
user: state.Auth.user,
isLoading: state.Auth.isLoading,
csrfToken: state.Auth.csrfToken
}
})
useEffect(() => {
dispatch(getUserAuth());
}, []);
console.log(userDetail, "143 App js")
return (
<>
{isLoading === true ? (
<View style={{
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
opacity: 0.6,
backgroundColor: 'white',
justifyContent: 'center',
alignItems: 'center',
zIndex: 1000
}}>
<ActivityIndicator size="large" color="black" />
</View>
) : null
}
<NavigationContainer>
{user !== null ? (
<RootStackScreen />
) : (
<AuthDStackScreen />
)}
</NavigationContainer>
</>
)
}

How to space around top tab bar in react native?

I am working on a dummy project for practice using react navigation material top tab. Everything is working fine, I just want to know is there any way to space around the tabs? I attached the output image of my code
Here is my code
import * as React from "react";
import { createDrawerNavigator } from "#react-navigation/drawer";
import AccountsScreen from "../screens/AccountsScreen";
import FavouritesScreen from "../screens/FavouritesScreen";
import HomeScreen from "../screens/HomeScreen";
import SettingsScreen from "../screens/SettingsScreen";
import TrendsScreen from "../screens/TrendsScreen";
import { createMaterialTopTabNavigator } from "#react-navigation/material-top-tabs";
import { Dimensions } from "react-native";
import Income from "../screens/Income";
import Expense from "../screens/Expense";
const Drawer = createDrawerNavigator();
const Tab = createMaterialTopTabNavigator();
CategoriesTabScreens = () => {
return (
<Tab.Navigator
initialRouteName="Income"
tabBarOptions={{
indicatorStyle: {
height: Dimensions.get("window").height,
backgroundColor: "#29416F",
},
activeTintColor: "#fff",
inactiveTintColor: "#333",
}}
>
<Tab.Screen name="Income" component={Income} />
<Tab.Screen name="Expense" component={Expense} />
</Tab.Navigator>
);
};
const AppDrawer = () => {
return (
<Drawer.Navigator
initialRouteName="Home"
screenOptions={{
headerShown: true,
}}
>
<Drawer.Screen name="Home" component={HomeScreen} />
<Drawer.Screen name="Accounts" component={AccountsScreen} />
<Drawer.Screen name="Categories" component={CategoriesTabScreens} />
<Drawer.Screen name="Trends" component={TrendsScreen} />
<Drawer.Screen name="Favourites" component={FavouritesScreen} />
<Drawer.Screen name="Settings" component={SettingsScreen} />
</Drawer.Navigator>
);
};
export default AppDrawer;
Output
the result I want
Just use Style prop in Tab.navigator
Here is snack
https://snack.expo.io/#anthowm/drawer-navigation-%7C-react-navigation
const Tab = createMaterialTopTabNavigator();
const CategoriesTabScreens = () => {
return (
<Tab.Navigator
initialRouteName="Income"
tabBarOptions={{
indicatorStyle: {
height: Dimensions.get("window").height,
backgroundColor: "#29416F",
},
activeTintColor: "#fff",
inactiveTintColor: "#333",
}}
style={{paddingHorizontal: 24, marginTop: 40}}
>
<Tab.Screen name="Income" component={() => (<View style={{marginTop: 20}}><Text>INCOME</Text></View>)} />
<Tab.Screen name="Expense" component={() => (<View style={{marginTop: 20}}><Text>EXPENSE</Text></View>)} />
</Tab.Navigator>
);
};

React Navigation: Bottom Tabs style

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}>

Resources