【问题标题】:Invariant Violation: Element type is invalid: expected a string , or a class/function , but got: undefined不变违规:元素类型无效:期望字符串或类/函数,但得到:未定义
【发布时间】:2020-02-20 06:28:25
【问题描述】:

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

【问题讨论】:

    标签: reactjs react-native mobile import


    【解决方案1】:

    您的导入错误。

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

    react 模块内没有名为 ViewText 的导出。

    【讨论】:

      【解决方案2】:

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

      查看React Native here中的组件和API

      解决方案

      react 中删除导入 ViewText

      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>
              )
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-10-05
        • 2019-11-13
        • 2019-12-31
        • 1970-01-01
        • 2017-01-09
        • 2019-05-09
        • 2020-03-11
        • 2020-06-27
        • 1970-01-01
        相关资源
        最近更新 更多