Element type is invalid, you likely forgot to export your component

梦想的初衷 提交于 2019-12-24 18:43:42

问题


I get the following error when trying to run my code on the iOS simulator. The error states that "Element type is invalid: expected a string (for built-in components) but got: undefined. You likely forgot to export your component from the file it's defined in."

I've seen on other people's posts with the same issue they needed to add either "export default App" to the end of the file or use "export default class" at the declaration of the class.

The tutorial I'm following is https://medium.com/@gurusundesh/getting-started-with-react-native-mapview-5e0bd73aa602. It could be that the article is old and react-native has changed since then.

Any suggestions on how to fix this would be much appreciated.

Thanks

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  View
} from 'react-native';
import { MapView } from 'react-native-maps';

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' +
    'Cmd+D or shake for dev menu',
  android: 'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <MapView style={styles.map}
          initialRegion={{
            latitude:10,
            longitude:20,
            latitudeDelta:30,
            longitudeDelta:40
          }}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
  map: {
    position: 'absolute',
    top: 0,
    bottom: 0,
    left: 0,
    right: 0,
  }
});

export default App;

回答1:


Change your import of MapView to:

import MapView from 'react-native-maps';

Per the docs: https://github.com/airbnb/react-native-maps



来源:https://stackoverflow.com/questions/46737778/element-type-is-invalid-you-likely-forgot-to-export-your-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!