组件标题中的图标未显示

时间:2019-08-23 00:37:58

标签: react-native react-navigation

我正在按照doc example的要求在组件标题中添加加号:

import React, { Component} from 'react';
import { SectionList, View, Image, StyleSheet, Text, TouchableOpacity, Platform, TouchableHighlight, AppRegistry } from 'react-native';
import {Icon } from "react-native-elements";

export default class Event extends React.Component {

    static navigationOptions = ({navigation}) => {
      console.log("in event header");
      return {
        headerTitle: 'Event',
        headerRight: (
          <TouchableOpacity>
            <Icon 
              name="plus" 
              size={30} 
              type='octicon' 
              onPress={navigation.getParam('navToNewEvent')}
              />
          </TouchableOpacity>
          ),
        };
    }

    _navToNewEvent = () => {
      console.log("Route to new event");
      this.props.navigation.navigate("NewEvent");
    };

    async componentDidMount(){
      this.props.navigation.setParams({ navToNewEvent: this._navToNewEvent })
    ....
    }

但是plus +没有显示在组件标题中:

enter image description here

上面的代码缺少什么?

更新:组件Event之前的导航代码是:

return createBottomTabNavigator(
          {
            Event: {screen: EventStack},
            Group: {screen: GroupStack},
            Contact: {screen: ContactStack},

          }, bottomTabNavOptions
      );

const bottomTabNavOptions =  {
  defaultNavigationOptions: ({ navigation }) => ({
    tabBarIcon: ({ focused, tintColor }) => {
      const { routeName } = navigation.state;
      console.log("route name", routeName);
      let iconName;
      if (routeName === 'Event') {
        iconName = `list-unordered`;
      } else if (routeName === 'NewEvent') {
        iconName = "kebab-horizontal";
      } else if (routeName === 'NewUser') {
        iconName = `person`;
      } else if (routeName === 'ListUser') {
        iconName = `organization`
      }

      return <Icon name={iconName} size={30} color={tintColor} type='octicon' />;
    },
  }),
  tabBarOptions: {
    activeTintColor: 'tomato',
    inactiveTintColor: 'gray',
  },
};

const EventStack = createStackNavigator({

      Event:  {
        screen: EventWithSelf,
        navigationOptions: {
          title: 'Event',
        },
      },
      NewEvent: {
        screen: NeweventWithSelf,
        navigationOptions: {
          title: 'New Event',
        },
      },

      EditEvent: {
        screen: EditEventWithSelf,
        navigationOptions: {
          title: 'Edit Event',
        },
      },

      Chat: {
        screen: ChatWithSocket,
        navigationOptions: {
          title: 'Chat',
        },

      },     
    });

1 个答案:

答案 0 :(得分:1)

仅查看代码似乎并没有错。有一个陷阱。您要修改吗?

static navigationOptions = ({navigation}) => {
      console.log("in event header");
      return {
        title: 'Event',
        headerRight: (
          <TouchableOpacity>
            <Icon 
              name="plus" 
              size={30} 
              type='octicon' 
              onPress={() => navigation.getParam('navToNewEvent')}
              />
          </TouchableOpacity>
          ),
        };
    }

您可以使用defaultNavigationOptions 您代表的“事件”标签不是一个屏幕。作为通用标题,请在defaultNavigationOptions中配置标题和按钮。

defaultNavigationOptions: ({ navigation }) => ({
...
 headerTitle: 'Event',
        headerRight: (
          <TouchableOpacity>
            <Icon 
              name="plus" 
              size={30} 
              type='octicon' 
              onPress={navigation.getParam('navToNewEvent')}
              />
          </TouchableOpacity>
          ),