I want my header icon to show count (every unread notification is count ++). From my Notification component I want to pass to the header in my Navigator a prop with the count number. All I can find so far are solutions for react navigation <5 (setParams and the like doesnt work with >5) and I am using 6. How do I do it?
const App = () => {
const [notifications, setNotifications] = useState([]);
useEffect(() => {
const notifications = await API.fetch();
let count= 0;
notifications.map(notification => {
if (notification.isRead === false) count ++;
})
// ---> send count to header in Navigator to be shown
}, [])
}
const MyTabs = (props) => {
const navigation = useNavigation();
return (
<Tab.Navigator
screenOptions={{
headerRight: (props) => {
return (
<TouchableOpacity onPress={() => navigation.navigate("Notifications")}>
<Ionicons name="notifications" size={24} color="black"/>
</TouchableOpacity>
)}
}}
>
</Tab.Navigator>
);
}
Edit:
const MyStack = () => {
const navigation = useNavigation();
return (
<Stack.Navigator>
<Stack.Group>
<Stack.Screen name="Tabs" options={({route}) => ({
headerShown: false,
})}>
{(props) => <MyTabs {...props} />}
</Stack.Screen>
<Stack.Screen
name="Notifications"
options={{
headerShown: true, headerLeft: () => (
<Button onPress={() => navigation.goBack()} title="go back"
/>
),
}}>
{(props) => <NotificationsScreen {...props}/>}
</Stack.Screen>
</Stack.Group>
</Stack.Navigator>
<NavigationContainer>
<MyStack />
</NavigationContainer>
//NavigationContainer is rendered inside App
MyTabsgets rendered in relation toApp? IsMyTabsa child ofApp?