I am trying to add an icon to the headerRight using react-navigation-header-buttons library. But the title of the icon is displaying instead of the icon.
This is the code where icon should display.
import React, { useState, useLayoutEffect } from 'react';
import { View, Text, Button, StyleSheet } from 'react-native'
import { MEALS } from '../data/DummyData';
import { HeaderButtons, Item } from 'react-navigation-header-buttons';
import { HeaderButton } from '../components/CustomHeaderButton';
const MealDetailScreen = props => {
const mealId = props.route.params.mealId;
const selectedMeal = MEALS.find(meal => meal.id == mealId);
const [headerTitle, setHeaderTitle] = useState();
useLayoutEffect(() => {
setHeaderTitle(() => selectedMeal === undefined ? props.route.params.title : selectedMeal.title)
props.navigation.setOptions({
headerTitle: headerTitle,
headerRight: () => (
<HeaderButtons HeaderButtonComponent={HeaderButton}>
<Item
title="Favourite"
iconName="ios-star"
onPress={() => {
console.log('Mark as favorite!');
}}
/>
</HeaderButtons>
)
});
}, [props.navigation, headerTitle]);
return (
<View>
<Text>The Meal Detail Screen!</Text>
<Text>{selectedMeal.title}</Text>
<Button title="Go Back to Categories!" onPress={() => {
props.navigation.popToTop();
}} />
</View>
)
}
const styles = StyleSheet.create({
});
export default MealDetailScreen;
This is the custom component for the header button.
import React from 'react';
import { Platform } from 'react-native';
import { HeaderButton } from 'react-navigation-header-buttons';
import { Ionicons } from '@expo/vector-icons';
import Colors from '../constants/Colors';
const CustomHeaderButton = props => {
return (
<HeaderButton
{...props}
IconComponent={Ionicons}
iconSize={23}
color={Platform.OS === 'android' ? 'white' : Colors.primaryColor}
/>);
}
export default CustomHeaderButton;
Only the title in the is displaying at the headerRight.
<Item
title="Favourite"
iconName="ios-star"
onPress={() => {
console.log('Mark as favorite!');
}}
/>
React Native version: 5 react-navigation-header-buttons version: 3.0.5
The most common way to interact with a header is by tapping on a button either to the left or the right of the title. Let's add a button to the right side of the header (one of the most difficult places to touch on your entire screen, depending on finger and phone size, but also a normal place to put buttons).
Here are the 3 Ways to Add Image Icon Inside Navigation Bar in React Native. If you want to make an application with a React Navigation, you can find a NavigationBar/ ActionBar on the top of the Screen. If you want to add the image/logo in Navigation Bar you can simply do it using headerLeft: <You Image component/>.
Alternatively, the headerLeft option also accepts a React Component, which can be used, for example, for overriding the onPress behavior of the back button. Read more about this in the api reference. You can set buttons in the header through the headerLeft and headerRight properties in options.
Displaying Image Icon in header bar is easy in latest 5.x react navigation version. This method is known as replacing header bar title bar Title with custom component in react native. Because by default only Title of activity screen will display on header bar but using headerLeft prop we can easily replace Title text with custom component.
We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run If you want to start a new project with a specific React Native version, you can use the --version argument:
The problem lies in your component import statement.
Instead of
import { HeaderButton } from '../components/CustomHeaderButton';
Type
import HeaderButton from '../components/CustomHeaderButton';
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