This is my navigation stack using react navigation v3.2.1:
I have a switch navigator to switch to Authentication navigation stack and Authenticated App stack.
The App stack is made using a bottom tab navigator.
I would like to use a custom component for the tab navigator.
How do I get the current routeName of the tab navigator when using createBottomTabNavigator
and a custom tabBarComponent
.
Eg:
AppContainer.js
const switchStack = createSwitchNavigator({
AuthLoading: AuthLoadingScreen,
App: AppStack,
Auth: AuthStack
}, {
initialRouteName: 'AuthLoading',
})
export default createAppContainer(switchStack)
AppStack.js
const AppStack = createBottomTabNavigator({
Home: {
screen: HomeStack,
},
Chat: {
screen: ChatStack
},
}, {
initialRouteName: 'Home',
activeColor: '#f0edf6',
inactiveColor: '#3e2465',
shifting: false,
barStyle: {
backgroundColor: '#694fad',
},
labeled: false,
tabBarComponent: ({navigation}) => <BottomBar navigation={navigation}/>
})
export default AppStack
BottomBar.js
export default class BottomBar extends React.Component {
constructor(props) {
super(props)
}
render() {
return (
<View style={styles.container}>
<IconComponent routeName={'Home'}/>
<IconComponent routeName={'Chat'}/>
</View>
)
}
}
IconComponent.js
export default class IconComponent extends React.Component {
constructor(props) {
super(props)
}
...
render() {
let IconComponent
let iconName
let iconSize = 25
switch (this.props.routeName) {
case 'Home':
IconComponent = MaterialCommunityIcons
// iconName = `home${focused ? '' : '-outline'}`;
iconName = `home`;
break
case 'Chat':
IconComponent = AntDesign
iconName = `message1`
iconSize = 22
break
}
let tintColor = 'green'
// if focused Home is current tab screen then change style eg. tint color.
// similary if current tab screen is Chat, then change style.
return (
<Animated.View
style={[
styles.container,
{
opacity: this.opacity
}
]}
>
<IconComponent name={iconName} size={iconSize} color={tintColor}/>
</Animated.View>
)
}
}
You can with navigation.state.routeName
tabBarComponent: ({navigation}) => <BottomBar navigation={navigation} currentRouteName={navigation.state.routeName} />
Or Better still you can do something like this:
const TabNavigator = createBottomTabNavigator({
Home: Home,
Chat: Chat
},
{
defaultNavigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, horizontal, tintColor }) => {
if (navigation.state.routeName === 'Home') {
return <Icon name='ios-home' size={30} color={tintColor} />
} else if (navigation.state.routeName === 'Chat') {
return <Icon name='ios-heart' size={30} color={tintColor} />
}
},
tabBarOptions: {
activeTintColor: '#2BEDBA',
inactiveTintColor: '#FAFAFA',
style: { backgroundColor: '#000', paddingTop: 5 }
},
})
});
navigation object of your custom BottomBar has an index that hold the current active screen index
tabBarComponent: ({navigation}) => <BottomBar navigation={navigation}/>
navigation.state.index
If Home screen is active >> navigation.state.index would be 0 If Chat screen is active >> navigation.state.index would be 1 ...etc
Using a functional component with React Navigation 5.x. You can use the useIsFocused hook.
import { useIsFocused } from "@react-navigation/native";
Usage: In each of the tab screens that you want to detect if they are the currently active or focused.
const isFocused = useIsFocused();
if (isFocused) {
// the screen is currently focused
// your code here
}
Docs: https://reactnavigation.org/docs/function-after-focusing-screen/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With