In a tab navigator I am defining I have access to the current active tab via props.children
if I pass props
to my CustomHeader
like so:
headerTitle: (props) => <CustomHeader {...props} />
… However, the issue is that in this scenario the navigation
object is not part of props
. And, to clarify, the navigation
object is coming in from outside this component.
If, on the other hand, I pass props like this:
headerTitle: () => <CustomHeader {...props} />
… then I do have the navigation
object as part of props
, but I no longer have access to props.children
.
My question is, how can I have access to both within my CustomHeader
?
Here is the bottomTabNavigator code:
export const ClientBottomTabNavigator = (props) => {
const Tab = createBottomTabNavigator();
return (
<Tab.Navigator
screenOptions={{
headerShown: true,
headerStyle: {
backgroundColor: Colors.primary,
elevation: 0,
shadowOpacity: 0,
shadowColor: 'transparent',
height: 155,
},
headerShadowVisible: false,
tabBarStyle: { backgroundColor: Colors.primary },
headerTintColor: Colors.light,
headerTitle: () => <CustomHeader {...props} />,
tabBarActiveTintColor: Colors.light,
tabBarInactiveTintColor: Colors.lightInactive,
tabBarActiveBackgroundColor: Colors.primary,
tabBarInactiveBackgroundColor: Colors.primary,
}}
initialRouteName={'Sessions'}
>
<Tab.Screen
name='Sessions'
component={SessionsTab}
options={{
tabBarLabel: 'SESSIONS',
tabBarIcon: ({ color, size }) => <FontAwesome name='street-view' color={color} size={size} />,
}}
/>
<Tab.Screen
name='Chart'
component={ChartTab}
options={{
tabBarLabel: 'CHARTS',
tabBarIcon: ({ color, size }) => <FontAwesome name='id-badge' color={color} size={size} />,
}}
/>
<Tab.Screen
name='Goals'
component={GoalsTab}
options={{
tabBarLabel: 'EDIT GOALS',
tabBarIcon: ({ color, size }) => <FontAwesome name='trophy' color={color} size={size} />,
}}
/>
</Tab.Navigator>
);
};
And here is the CustomHeader
code:
const CustomHeader = (props) => {
const patient = useSelector(selectActivePatient);
const dispatch = useDispatch();
return (
<View>
<View style={{ width: '100%', flexDirection: 'row', justifyContent: 'space-between', marginBottom: 4 }}>
<Feather
name='chevron-left'
size={24}
onPress={() => {
props.navigation.navigate('Main'); // Here is where I want to be able to customize the back button based on the currentTab
}}
color={styles.colors.textInverse}
style={{ minWidth: '8%', justifySelf: 'flex-start', alignSelf: 'flex-start' }}
/>
<Text
style={{
color: Colors.light,
fontSize: 18,
alignSelf: 'center',
justifySelf: 'center',
fontWeight: 'bold',
}}
>
{patient?.firstName} {patient?.lastName}
</Text>
<Feather
name='' // Leave this blank
style={{ minWidth: '8%', justifySelf: 'flex-end', alignSelf: 'flex-end' }}
/>
</View>
<View style={{ alignItems: 'center', paddingBottom: 6 }}>
<Text style={{ color: '#fff', fontSize: 18, marginBottom: 6 }}>{convertService(patient?.service)}</Text>
<View>
<TextInput
style={{
color: Colors.light,
fontSize: 16,
width: '100%',
textAlign: 'center',
}}
placeholder='Add a note...'
placeholderTextColor={styles.colors.textPlaceholder}
maxLength={50}
onChangeText={(text) => dispatch(updateNote(patient?.id, text))}
defaultValue={patient?.note?.value}
/>
</View>
</View>
</View>
);
};
2
Answers
Pass it as a separate property, for example like this:
You could give the
CustomHeader
some kind oftitle
prop and pass the children of theheaderTitle
props.And in the CustomHeader