Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Icon is not displaying in react-navigation-header-buttons (React Native)

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

like image 411
Denver Shan Avatar asked Apr 12 '20 17:04

Denver Shan


People also ask

How do I use the react navigation header buttons?

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).

How to add image icon inside navigation bar in React Native?

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/>.

How to set buttons in the header of a React 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.

How to replace header bar title title with custom component in React Native?

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.

How to make a react native app with React Native CLI?

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:


Video Answer


1 Answers

The problem lies in your component import statement.

Instead of

import { HeaderButton } from '../components/CustomHeaderButton';

Type

import HeaderButton from '../components/CustomHeaderButton';
like image 96
DaveK Avatar answered Sep 28 '22 10:09

DaveK