React Native 错误:元素类型无效:需要字符串或类/函数但得到:对象

新手上路,请多包涵

我遇到了这个错误,我在解决这个问题时遇到了很多麻烦。

我想在这里做的是有 3 个不同的屏幕,并有一个导航到每个屏幕的标签栏。

这是我的索引:

 import React, { Component } from 'react';
import { AppRegistry, Navigator, StyleSheet, View, Text } from 'react-native';

import Nav from './app/components/Nav';
import Screen from './app/Screen';

import Tabs from 'react-native-tabs'

import SwitchView from './SwitchView';

class Proj extends Component {

constructor(props){
    super(props);
}

render(){
    var x = "FeedView";
    return(

          <View style={styles.container}>
            <Tabs selected={x} style={{backgroundColor:'white'}}
                  selectedStyle={{color:'red'}} onSelect={el=> {x = el.props.name}}>
                <Text name="FeedView">First</Text>
                <Text name="WikiView" selectedIconStyle={{borderTopWidth:2,borderTopColor:'red'}}>Second</Text>
                <Text name="BoardView">Third</Text>
            </Tabs>

            <SwitchView id={x}/>

          </View>
    );
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
})

AppRegistry.registerComponent('Proj', () => Proj);

这是我的 SwitchView:

 import Feed from './app/pages/Feed/Feed';
import Board from './app/pages/Board';
import Wiki from './app/pages/Wiki';

class SwitchView extends Component {
render(){
    var { id } = this.props;

    switch (id) {
        case "FeedView":
            return <Feed/>
        case "WikiView":
            return <Wiki/>
        case "BoardView":
            return <Board/>
    }
}
}

原文由 Parkicism 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 458
1 个回答

这可能是由程序中的某些 JS 模块导出/导入问题引起的,通常是由于以下两个原因之一:

  • 您忘记导出,或者导出的内容不正确
  • 你导入了不存在的东西,或者你导入了不正确的东西

我遇到了类似的错误,但就我而言,它不是由 export 引起的,而是由 import 引起的,我使用了 import 语句错误地导入了某些东西-模块中不存在的。

在我的例子中, import 被错误地写成:

import { MyComponent } from './MyComponents/MyComponent'

而实际上它应该是:

import MyComponent from './MyComponents/MyComponent'

这让我发疯了,花了我一整天的时间才弄明白,我希望这能为一些人节省几个小时。

原文由 nybon 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题