不变违规:元素类型无效:应为字符串或类/函数,但得到:未定义

时间:2020-02-20 06:28:25

标签: reactjs react-native mobile import

App.js:

import React from 'react';
import Home from './screens/home';
import Navigator from './routes/drawer';
import Login from './screens/login';

export default class App extends React.Component{

    render() {
        if (false) {
            return (
                <Navigator />
            );
        } else {
            return (
                <Login />
            );
        }
    }
}

login.js:

import React, {View, Text} from 'react';

export default class Login extends React.Component {

    render() {
        return (
            <View style={{ padding: 20 }}>
                <Text>Some text</Text>
            </View>
        )
    }
}

该程序给我标题中的错误,它确实适用于Navigator,但不适用于Login(当if设置为true而不是false时,程序可以正常运行。)Navigator充当了典型的抽屉容器一个Android应用程序,但在导入的底部,所有内容似乎都与渲染选项的行为相同。完整的错误日志在下面...

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

Check the render method of 'Login'.

This error is located at:
in Login...
in App...RCTView...AppContainer...

2 个答案:

答案 0 :(得分:1)

您的进口商品是错误的。

import React from 'react';
import { View, Text } from 'react-native';

View模块内没有名为Textreact的导出。

答案 1 :(得分:0)

检查您的Login.js文件。有些进口是错误的!

检查React Native here中的组件和API

解决方案

反应

中删除导入查看文本
import React from 'react';
import { View, Text } from 'react-native';

export default class Login extends React.Component {

    render() {
        return (
            <View style={{ padding: 20 }}>
                <Text>Some text</Text>
            </View>
        )
    }
}