javascript - TabNavigator自定义图标在react-native中报错

标签 javascript android ios react-native tabnavigator

我正在使用 Xcode 10 和最新的 react-native 版本。 我使用 TabNavigator 创建了 StackNavigator 应用。

代码:navigation.js

import React from "react";
import { TabNavigator, StyleSheet, Text, View, Image} from "react-navigation";

import Dashboard from '.././Screen/Dashboard'
import Home from '.././Screen/Home'
import Events from '.././Screen/Events'
import Settings from '.././Screen/Settings'


export default Tab = TabNavigator({
  Home: {
    screen: Home,
  },
  Settings: {
    screen: Settings,
    navigationOptions: {
          tabBarLabel: 'Settings',
          tabBarIcon: ({ tintColor }) => (
            <Image source={require('.././assets/setting.png')}
            style= {{width:15, height:15, tintColor:'black'}}>
            </Image>
        )
    },
  },
  Events: {
    screen: Events,
    },
  }, {
  tabBarPosition: 'bottom',
  swipeEnabled: true,
  tabBarOptions: {
    showIcon: true,
    activeTintColor: '#f2f2f2',
    activeBackgroundColor: "#2EC4B6",
    inactiveTintColor: '#666',
    labelStyle: {
      fontSize: 16,
      padding:4,
    }
  }
});

但是我这里有错误,

[fatal][tid:com.facebook.react.ExceptionsManagerQueue] Unhandled JS Exception: Invariant Violation: Invariant Violation: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of TabBarIcon.

如果我删除这一行:

tabBarIcon: ({ tintColor }) => (
                <Image source={require('.././assets/setting.jpeg')}
                style= {{width:15, height:15, tintColor:'black'}}>
                </Image>
            )

那么它在没有图标的情况下也能完美运行。 我搜索了所有内容,但没有找到解决方案。

最佳答案

请试试这个(假设你创建了一个底部导航器并且你有最新的 react 导航)

import { createBottomTabNavigator } from 'react-navigation';

export default createBottomTabNavigator({
  Home: {
    screen: Home,
  },
  Settings: {
    screen: Settings,
    navigationOptions: {
          tabBarLabel: 'Settings',
          tabBarIcon: ({ tintColor }) => (
            <Image source={require('.././assets/setting.png')}
            style= {{width:15, height:15, tintColor:'black'}}>
            </Image>
        )
    },
  },
  Events: {
    screen: Events,
    },
  }, {
  tabBarPosition: 'bottom',
  swipeEnabled: true,
  tabBarOptions: {
    showIcon: true,
    activeTintColor: '#f2f2f2',
    activeBackgroundColor: "#2EC4B6",
    inactiveTintColor: '#666',
    labelStyle: {
      fontSize: 16,
      padding:4,
    }
  }
});

关于javascript - TabNavigator自定义图标在react-native中报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52968204/

相关文章:

javascript - 如何让 Node.js 在我的服务器上运行?

ios - 如何恢复 Cordova iOS 应用程序

ios - 时间格式(swift 4 中的 hh :mm a) not working for iOS 11. 1.1

javascript - 初始化动态创建的 btn 单选按钮组

javascript - 使用 reanimated、redash 来 react 原生动画 onScroll 的问题

javascript - 是否可以将 Vue 应用程序部分渲染到多个节点?

android - fragment 中的 getPackageName()

android - 如何将 Listview 的内容发送到另一个 Activity?

java - 在父类(super class)构造函数中将 'this' 转换为子类

ios - Swift 中的表单验证