如何在 Android 中正确使用 React Native 中的仅 iOS 组件而不会出错
How to properly use iOS only components in React Native without error in Android
我是 React Native 的新手,正在测试 PushNotificationIOS。但是 checkpermission 调用在 Android.
上给我错误
ExceptionsManager.js:61 Cannot read property 'checkPermissions' of undefined
我猜这是因为我只需要在 iOS 上使用该组件。如何添加 OS 的检查以仅在 iOS 上进行调用?
这是我的代码:
componentWillMount: function() {
//-- need an OS check here??
PushNotificationIOS.checkPermissions((data)=> {
console.log("in comp will mount: checking for permission")
console.log(data.alert)
console.log(data.badge)
我建议将特定于平台的代码拆分到单独的文件中。
React Native will detect when a file has a .ios. or .android.
extension and load the relevant platform file when required from other
components.
MyFile.ios.js
MyFile.android.js
然后您可以按如下方式要求该组件:
const MyFile= require('./MyFile');
并像这样使用它
componentWillMount: function() {
//-- it would call the logic of what ever platform was detected automatically
MyFile.CallWhatEver();
并且它将 运行 平台特定代码。
另一种方法是
平台模块
React Native provides a module that detects the platform in which the
app is running. You can use the detection logic to implement
platform-specific code. Use this option when only small parts of a
component are platform-specific.
if(Platform.OS === 'ios')
还有一个platform.select
可以接受任意值
const Component = Platform.select({
ios: () => //function goes here,
android: () => require('ComponentAndroid'),
})();
link
https://facebook.github.io/react-native/docs/platform-specific-code.html
我是 React Native 的新手,正在测试 PushNotificationIOS。但是 checkpermission 调用在 Android.
上给我错误ExceptionsManager.js:61 Cannot read property 'checkPermissions' of undefined
我猜这是因为我只需要在 iOS 上使用该组件。如何添加 OS 的检查以仅在 iOS 上进行调用?
这是我的代码:
componentWillMount: function() {
//-- need an OS check here??
PushNotificationIOS.checkPermissions((data)=> {
console.log("in comp will mount: checking for permission")
console.log(data.alert)
console.log(data.badge)
我建议将特定于平台的代码拆分到单独的文件中。
React Native will detect when a file has a .ios. or .android. extension and load the relevant platform file when required from other components.
MyFile.ios.js
MyFile.android.js
然后您可以按如下方式要求该组件:
const MyFile= require('./MyFile');
并像这样使用它
componentWillMount: function() {
//-- it would call the logic of what ever platform was detected automatically
MyFile.CallWhatEver();
并且它将 运行 平台特定代码。
另一种方法是
平台模块
React Native provides a module that detects the platform in which the app is running. You can use the detection logic to implement platform-specific code. Use this option when only small parts of a component are platform-specific.
if(Platform.OS === 'ios')
还有一个platform.select
可以接受任意值
const Component = Platform.select({
ios: () => //function goes here,
android: () => require('ComponentAndroid'),
})();
link https://facebook.github.io/react-native/docs/platform-specific-code.html