有没有一种方法可以有条件地渲染一个 node_module?

huangapple go评论63阅读模式
英文:

Is there a way to conditionally render a node_module

问题

I am trying to use react-native-maps on iOS and Android but I also want my code to work on the web. I want to disable this feature on the web but make it work on iOS and Android. Is there a way to do this?

I have tried this:

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);
  });
}

In a useEffect without any luck.

英文:

I am trying to use react-native-maps on ios and andriod but I also want my code to work on web. I want to disable this feature on web but make it work on ios and andriod Is there a way to do this?

I have tried this

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);
      });
    }

In a useEffect without any luck

答案1

得分: 1

我认为你应该查看文档中的“平台特定代码”部分,特别是文件扩展名部分。

https://reactnative.dev/docs/platform-specific-code#platform-specific-extensions

也许在你的情况下,你可以有一个名为BaseComponent.js的文件,其中包含Web的Component.js和移动端的Component.native.js?

英文:

I think you should take a look to the "platform specific code" part of the doc, especially the file extension part.

https://reactnative.dev/docs/platform-specific-code#platform-specific-extensions

Maybe in your case you could have a BaseComponent.js with Component.js for web and Component.native.js for mobile ?

huangapple
  • 本文由 发表于 2023年3月23日 08:41:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定