javascript - 尝试使用自定义图标创建 TabBarIOS 时发生不变冲突错误

标签 javascript react-native tabbar mobile-application

我在这里主要想完成的是合并这两个教程 (x) (x)为了创建一个具有自定义图标的简单 TabBar。我正在尝试使用 react-native-vector-icons 库中的图标,我已将其添加到我的节点模块中。但是,我遇到了一个障碍:

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

This error is located at: in RCTTabBar (at TabBarIOS.ios.js:82)

in TabBarIOS (at App.js:19)

in ProfileTabs (at App.js:80)

in ProfileApp (at registerRootComponent.js:35)

in RootErrorBoundary (at registerRootComponent.js:34)

等 这是相关代码:

import React, { Component } from 'react';
import { AppRegistry, Image, StyleSheet, TabBarIOS, Text, View } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';

var Profile = require('./profile');
var Repositories = require('./repositories');
var Following = require('./following');
var Followers = require('./followers');

class ProfileTabs extends Component {
    constructor(props) {
        super(props);
        this.state = {
            selectedTab: 'profile'
        };
    }
    render() {
        return (
            <TabBarIOS
                selectedTab={this.state.selectedTab}>
                <Icon.TabBarIOS
                    selected={this.state.selectedTab === 'profile'}
                    title="Profile"
                    iconName='ios-home-outline'
                    onPress={() => {
                        this.setState({
                            selectedTab: 'profile',
                        });
                    }}>
                        <Profile/>
                </Icon.TabBarIOS>
                <Icon.TabBarIOS
                    selected={this.state.selectedTab === 'repositories'}
                    title='Repos'
                    iconName='ios-home-outline'
                    onPress={() => {
                        this.setState({
                            selectedTab: 'repositories',
                        });
                    }}>
                        <Repositories/>
                </Icon.TabBarIOS>
                <Icon.TabBarIOS
                    selected={this.state.selectedTab === 'following'}
                    title='Following'
                    iconName='ios-home-outline'
                    onPress={() => {
                        this.setState({
                            selectedTab: 'following',
                        });
                    }}>
                    <Following/>
                </Icon.TabBarIOS>
                <Icon.TabBarIOS
                    selected={this.state.selectedTab === 'followers'}
                    title='Followers'
                    iconName='ios-home-outline'
                    onPress={() => {
                        this.setState({
                            selectedTab: 'followers',
                        });
                    }}>
                    <Followers/>
                </Icon.TabBarIOS>
            </TabBarIOS>
        )
    }
}

export default class ProfileApp extends Component {
    render() {
        let pic = {
            uri: 'https://news.artnet.com/app/news-upload/2015/08/mona-lisa_2-e1440166499927.jpg'
        };
        return (
            <View style={styles.basic_info}>
                <Image source={pic} style ={styles.circle_image}/>
                <Text style={styles.name_text}>LM</Text>
            </View>,
            <ProfileTabs/>
        );
    }
}

我尝试了一些修复 here ,但是对于那里的很多答案,我不确定给定解决方案背后的推理,并且对于如何将其应用于我自己的代码感到困惑。如果我使用 TabBarIOS.Item 元素而不是 Icon.TabBarIos 元素,并使用 SystemIcons 而不是库图标,一切正常,所以我怀疑问题在于我如何导入/使用 react-native-vector-icons。

如果有更简单的方法来完成这一切,我也愿意重写我的代码。

最佳答案

如库代码中所述Ionicons.js TabBarIOS 没有命名导出,但TabBarItemIOSTabBarItem

因此您需要将其用作

  <TabBarIOS>
    <Icon.TabBarItemIOS
      title="Home"
      iconName="ios-home"
      selected={this.state.selectedTab === 'home'}
      onPress={() => {
        this.setState({
          selectedTab: 'home',
        });
      }}
    >
      {this.renderContent('#414A8C', 'Home')}
    </Icon.TabBarItemIOS>

即将 Icon.TabBarIOS 替换为 Icon.TabBarItemIOS

查看示例 here了解更多详情

关于javascript - 尝试使用自定义图标创建 TabBarIOS 时发生不变冲突错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49460943/

相关文章:

swift - 如何快速更改标签栏的色调颜色?

android - 如何在React Native Expo中的Dropdown选项中动态获取api数据

react-native - TouchableOpacity 中的 onPress 不会触发

javascript - 乘法 2 数组 javascript/jquery

javascript - 如何在 HTML 文档中使用 javascript 创建一个简单的成本计算器

javascript - 如何在 React Native 中创建 log.txt 文件?

快速更改 NavigationControl 和 TabBar 的颜色

ios - 标签栏项目的重新加载角标(Badge)

javascript - Socket.io 更改馈送在刷新/重新加载时多次发出

javascript - 在phonegap应用程序中使用浏览器本地存储