I'm trying to set the navigation options from my stack screen inside my component since I need to render a right header button with specific actions from my screen. Problem is that it is not working. The structure I'm using is as follows:
-StackNavigator
-MaterialTopTabNavigator
-tab1
-tab2
-tab3
Current code:
//My navigation
<NavigationContainer initialState={initialState} ref={ref}>
<Stack.Navigator screenOptions={{ headerShown: false }}>
//some other Stacks
<Stack.Screen
name="Detail"
component={DetailTabScreen}
options={{
headerShown: true, //header works ok
headerRight: () => ( //this would work
<View style={{backgroundColor: 'red', width: 100}}/>
)
}}
/>
</Stack.Navigator>
</NavigationContainer>
My DetailTabScreen:
const TopTab = createMaterialTopTabNavigator()
const DetailTabScreen = () => (
<StoreProvider>
<SafeAreaView style={{ flex: 1 }}>
<TopTab.Navigator
tabBarOptions={{
...options
}}
>
<TopTab.Screen
name="PlanDetail"
component={PlanDetail}
options={{
tabBarLabel: ({ color }: TabBarLabelProps) => (
<Text>Detalles</Text>
),
}}
/>
<TopTab.Screen
name="PlanChat"
component={PlanChat}
options={{
tabBarLabel: ({ color }: TabBarLabelProps) => (
<Text>Chat</Text>
),
}}
/>
<TopTab.Screen
name="Participants"
component={Participants}
options={{
tabBarLabel: ({ color }: TabBarLabelProps) => (
<Text>Participantes</Text>
),
}}
/>
</TopTab.Navigator>
</SafeAreaView>
</StoreProvider>
)
I already have tried setting the options inside the component as the docs suggests using the useLayoutEffect
, also tried using the useEffect
:
useLayoutEffect(() => {
props.navigation.setOptions({
headerRight: () => (//does not work
<TouchableOpacity
onPress={() => console.warn('This is a button!')}
style={{marginRight:16}}
>
<Icon name={Platform.OS === 'ios' ? 'share-apple' : 'share-google'} type="EvilIcons" style={{ color: colors.darkGreen, marginright: 16 }} />
</TouchableOpacity>
),
});
}, []);
PS: I know that hiding the header and using a custom component for my header would work as a work around but I want to know why navigation.setOptions
is not working.
Any help, any ideas would be appreciated, thanks!