【问题标题】:React-Native : error while import component to App.jsReact-Native:将组件导入 App.js 时出错
【发布时间】:2018-08-31 07:11:00
【问题描述】:

从组件导入默认类时出错 使用expo,我搜索了这个错误但没有解决解决方案,组件返回对象。

type is invalid -- expected a string (for built-in components) or a class/function (for composite components) 但得到:对象

错误:

12:19:03: Warning: React.createElement: 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, or you might have mixed up default and named imports.

Check your code at registerRootComponent.js:35.
    in ExpoRootComponent (at renderApplication.js:33)
    in RCTView (at View.js:60)
    in View (at AppContainer.js:102)
    in RCTView (at View.js:60)
    in View (at AppContainer.js:122)
    in AppContainer (at renderApplication.js:32)

App.js

import Login from './app/components/login';

app/components/login/index.js

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

export default class Login extends Component {
    constructor(props){
        super(props);
        this.state = {}
    }
    render(){
        return(
            <View>
             <Text>Login</Text>
            </View>
        )
    }
}

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:

    App.js 中使用Login 组件的默认导入。另外,从App.js导出一个根组件

    import React from "react";
    import Login from './app/components/login';
    
    export default class App extends React.Component {
      render() {
        return <Login/>;
      }
    }
    

    【讨论】:

    • 没什么,只有一行import。
    猜你喜欢
    • 2021-03-30
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-28
    相关资源
    最近更新 更多