React Native 有没有一种方法可以有条件地渲染node_module

hjzp0vay  于 2023-03-24  发布在  React
关注(0)|答案(1)|浏览(81)

我试图在iOS和andriod上使用react-native-maps,但我也希望我的代码在Web上工作。我想在Web上禁用此功能,但使其在iOS和andriod上工作。有没有办法做到这一点?
我试过这个

if (Platform.OS === 'android' || Platform.OS === 'ios') {
      Promise.all([
        import('react-native-maps').then((module) => module.default),
        import('react-native-maps').then((module) => module.Marker),
      ]).then(([MapView, Marker]) => {
        setMapView(MapView);
        setMarker(Marker);
      });
    }

在一个useEffect没有任何运气

iqih9akk

iqih9akk1#

我认为你应该看看文档的“平台特定代码”部分,特别是文件扩展名部分。
https://reactnative.dev/docs/platform-specific-code#platform-specific-extensions
也许在你的情况下,你可以有一个BaseComponent.js与Component.js为Web和Component.native.js为移动的?

相关问题