如何在导出中添加“ requireNativeComponent”?

时间:2020-02-05 19:27:13

标签: reactjs react-native maps react-native-maps

这是我第一个React Native应用程序。我正在尝试从react-native-maps中使用MapView,但是遇到此错误:“尝试导入错误:'requireNativeComponent'并未从'react-native-web / dist / index'中导出。” 我正在网络视图中查看我的应用程序(如果有的话)。我已经安装并链接了react-native-maps。这是我当前的代码:

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

export default function App() {
  return (
    <View style={styles.container}>
      <Text>Open up App.js to start working on your app!</Text>
    </View>
  );
}

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

错误:

C:/React/AwesomeProject/node_modules/react-native-maps/lib/components/MapView.js
Attempted import error: 'requireNativeComponent' is not exported from 'react-native-web/dist/index'.

C:/React/AwesomeProject/node_modules/react-native-maps/lib/components/decorateMapComponent.js       
Attempted import error: 'requireNativeComponent' is not exported from 'react-native-web/dist/index'.

C:/React/AwesomeProject/node_modules/react-native-maps/lib/components/decorateMapComponent.js       
Attempted import error: 'requireNativeComponent' is not exported from 'react-native-web/dist/index'.

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,最后我意识到这是由于MapView与Web浏览器的兼容性,您可以在Platform Compatibility中进行检查。
可能您正在尝试在Web浏览器中运行此代码,因此由于MapView导入,您将遇到此错误。
我建议两个选择:
1-尝试将此脚本添加到index.html,用您的Google Maps API替换API_KEY:

<script
  async
  defer
  src="https://maps.googleapis.com/maps/api/js?key=API_KEY"
  type="text/javascript"
></script>

2-使用expo更改在某些移动设备中测试和运行应用程序的方式,通常可以正常运行。