我可以在React Native中专门为iOS设置一个模块吗? [英] Can I require a module specifically for iOS in React Native?
问题描述
我目前正在使用 react-native-safari-view <我的React Native项目中的/ code>
模块用于在iOS中显示Web视图。
I am currently using react-native-safari-view
module in my React Native project for showing web views in iOS.
由于该模块尚未针对Android实现,当我尝试为Android构建项目,它在这一行给我一个错误:
As the module is not yet implemented for Android, when I try to build the project for Android, it gives me an error at this line:
import SafariView from 'react-native-safari-view'
我将使用 链接
库,但我不知道如何使用两个平台的代码相同。
I am going to use the Linking
library for Android, but I don't know how to use the same code for two platforms.
我试过:
if (Platform.OS == 'ios') {
import SafariView from 'react-native-safari-view'
}
它给了我这个错误:
import'和'export'可能只出现在顶级的vel
import' and 'export' may only appear at the top level
我如何解决这个问题?
推荐答案
为了解决这个问题,我一直在使用 require
(但主要是模块而不是组件):
To get around this I have been using require
instead (but mainly for modules rather than components):
var SafariView;
if (Platform.OS == 'ios') {
SafariView = require('react-native-safari-view');
}
对于这种特殊情况,我肯定会选择Konstantin Kuznetsov的方法 - 坚持这个在这里,因为它可能会帮助其他人使用单独的文件制作包装器组件可能有点过分:)
For this particular situation I would definitely go for Konstantin Kuznetsov's approach - Just sticking this here as it might help someone else where making a wrapper component with separate files may be overkill :)
这篇关于我可以在React Native中专门为iOS设置一个模块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!