元素类型无效:期望得到一个字符串/类/函数:object

时间:2017-04-15 16:39:51

标签: javascript react-native

我正在研究React-Native项目,并在尝试实施反应导航时遇到问题。 我按照教程进行操作,在尝试运行项目时,我收到一条错误消息:

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it’s defined in.

我在网上搜索过,包括这个网站,但无法找到任何解决方案。 这是我的代码: index.js:

import React, { Component } from 'react';
import { Stack } from './config/Router';

class Application extends Component{
  render(){
    <View>
      return <Stack />;
    </View>
  }
}

export default Application;

这是路由器:

import React from 'react';
import { StackNavigator } from 'react-navigation';

import Login from '../components/Login';
import Home from '../components/Home';

export const Stack = StackNavigator({
  Login:{
    screen: Login
  },
  Home:{
    screen: Home,
    navigationOptions: {
      title: 'Home'
    }
  }
});

这是index.ios.js:

import { AppRegistry } from 'react-native';
import Application from './index'

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

如何修复错误?提前谢谢!

1 个答案:

答案 0 :(得分:1)

看起来像是

class Application extends Component{
  render(){
    <View>
      return <Stack />;
    </View>
  }
}

应该是

class Application extends Component{
  render(){
    return(
      <View>
        <Stack />
      </View>
    );
  }
}

此外,您还需要在index.js中导入View组件

You likely forgot to export your component from the file it’s defined in.

这几乎总是导致无效元素错误的原因,因此请务必仔细检查导入。