英文:
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 ?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论