【问题标题】:Having trouble importing < MapView > from 'expo'从 'expo' 导入 <MapView> 时遇到问题
【发布时间】:2020-02-19 20:20:47
【问题描述】:

我一直在关注一个关于尝试通过使用 react native 和 expo 在移动应用程序上实现地图的教程,但是在运行代码后,我不断收到多个错误消息,而提供教程的 youtuber 没有体验到这些错误消息。

疑似问题

错误似乎是因为我试图从“expo”导入,但我不认为 expo 组件导出地图视图?

尝试的解决方案

npm install expo --save

面临的错误

Invariant Violation:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。

查看App的渲染方法。

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

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = 
    {
      region : 
      {
        latitude: 1.290270,
        longitude: 103.851959,
        latitudeDelta: 0.02,
        longitudeDelta: 0.02,
      }
    }
  }
  render()
  {
    return (
      <View style={styles.container}>
        <Text>HomeScreen!</Text>
        <MapView 
        initialRegion = {this.state.region}
        showsUserLocation = {true}
        showsCompass = {true}
        rotateEnabled = {false}
        style = {{flex:1}}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
  },
});

【问题讨论】:

    标签: javascript react-native expo


    【解决方案1】:

    我能够通过运行解决类似的问题(在 iOS 模拟器上):

    expo install react-native-maps

    然后将导入更改为:

    import MapView from "react-native-maps";

    安装和使用说明可以在 MapView 组件的 Expo 文档页面上找到:https://docs.expo.io/versions/latest/sdk/map-view/

    【讨论】:

      猜你喜欢
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 2021-11-20
      相关资源
      最近更新 更多