简体   繁体   English

为什么会出现“元素类型无效”错误?

[英]Why am I getting “Element type is invalid” error?

In the error I'm getting in my iOS simulator doesn't make sense to me. error是我进入iOS模拟器对我来说没有意义。 My Home.js component seems fine to me. 我的Home.js组件对我来说似乎很好。 I can't understand how I'm getting this error. 我不明白如何收到此错误。 I've clearly exported the component. 我显然已经导出了该组件。 How can I get rid of this error? 如何摆脱这个错误?

Here's Home.js : 这是Home.js

import React from 'react';
import Container from 'native-base';
import {MapContainer} from "../../../components/MapContainer/index";

class Home extends React.Component {

    componentDidMount() {
        this.props.setName();
    }

    render() {
        const region = {
            latitude: 3.146642,
            longitude: 101.695845,
            latitudeDelta: 0.8922,
            longitudeDelta: 0.0421
        }
        return(
            <Container>
                <MapContainer region={region}/>
            </Container>
        );
    }
}

export default Home;

Here's index.js : 这是index.js

import React from 'react';
import View from 'native-base';
import MapView from 'react-native-maps';
import styles from './MapContainerStyles';

export const MapContainer = ({region}) => {
    return(
        <View style={styles.container}>
            <MapView
                provider={MapView.PROVIDER_GOOGLE}
                style={styles.map}
                region={region}
            >
            </MapView>
        </View>
    );
}

Here's the error : 这是error

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.

Check the render method of 'Home'.

you have two exports here 你这里有两个出口

import React from 'react';
import View from 'native-base';
import MapView from 'react-native-maps';
import styles from './MapContainerStyles';

//delete export from the next line
export const MapContainer = ({region}) => {
    return(
        <View style={styles.container}>
            <MapView
                provider={MapView.PROVIDER_GOOGLE}
                style={styles.map}
                region={region}
            >
            </MapView>
        </View>
    );

}

export default MapContainer;

You have 2 exports in your MapContainer . 您的MapContainer有2个导出。

You have this one at the top export const MapContainer 您在顶部export const MapContainer拥有一个

and you have this one at the bottom. 并且您在底部有这个。 export default MapContainer;

Now you need to get rid of one, but the one you keep will determine how you import it later. 现在您需要删除其中一个,但是保留的一个将决定以后如何导入它。

If you keep the default export you then import like so 如果保留默认导出,则可以像这样导入

import MapContainer from "../../../components/MapContainer/index";

If you keep the non default export you will import like so 如果保留非默认导出,则将这样导入

import {MapContainer} from "../../../components/MapContainer/index";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道为什么我收到以下错误“错误:元素类型无效:” - I can't figure out why I am getting the following error "Error: Element type is invalid:" React Element Type 无效,为什么会出现此错误以及如何解决? - React Element Type is Invalid, why am I getting this error and how do I solve it? 为什么我会收到此错误“无效参数”? - Why am I getting this error “Invalid argument”? 为什么我收到无效值错误? - Why am I getting an invalid value error? 为什么会出现“无效的正则表达式标志”错误? - Why am I getting an “Invalid regular expression flags” error? 为什么我会收到此错误 chrome-extension://invalid - Why am I getting this error chrome-extension://invalid 为什么我收到错误:无效的挂钩调用? - Why am I Getting the Error: Invalid Hook Call? 为什么在以下代码中出现未捕获的类型错误? - Why am I getting an uncaught type error with the following code? 为什么我收到类型错误:“function_name”不是一个函数 - Why am I getting a type error : "function_name" is not a function at 为什么出现此类型错误:实例与原型 - Why am I getting this type error: instance vs prototype
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM