首页 > 解决方案 > 如何根据平台导入模块

问题描述

我想根据平台导入一个模块。

例如:

import MapViewIOS from 'react-native-map-clustering'
import MapViewAndroid from 'react-native-maps'

const MapView = Platform.select({
  ios: MapViewIOS,
  android: MapViewAndroid,
})

错误:

JSX 元素类型“MapView”没有任何构造或调用 signatures.ts(2604)

标签: typescriptreact-native

解决方案


在 react-native 中,您应该按平台分隔文件。例如,使用MapView.ios.jsMapView.android.js名称创建两个文件,以便为平台和任何需要使用 MapView 的地方导出特定组件,像这样导入它们import MapView from "./MapView"


推荐阅读