mirror of
https://github.com/status-im/react-native.git
synced 2025-01-18 05:23:26 +00:00
3603479471
Summary:Version exposes the sdk level (which TBH is more useful anyways), not a version string. Closes https://github.com/facebook/react-native/pull/6068 Differential Revision: D2971198 fb-gh-sync-id: 0ba1e10e48b2ca51c7b0cebcc1ec13d0b69df783 shipit-source-id: 0ba1e10e48b2ca51c7b0cebcc1ec13d0b69df783
68 lines
2.0 KiB
Markdown
68 lines
2.0 KiB
Markdown
---
|
|
id: platform-specific-code
|
|
title: Platform Specific Code
|
|
layout: docs
|
|
category: Guides
|
|
permalink: docs/platform-specific-code.html
|
|
next: native-modules-ios
|
|
---
|
|
|
|
When building a cross-platform app, the need to write different code for different platforms may arise. This can always be achieved by organizing the various components in different folders:
|
|
|
|
```sh
|
|
/common/components/
|
|
/android/components/
|
|
/ios/components/
|
|
```
|
|
|
|
Another option may be naming the components differently depending on the platform they are going to be used in:
|
|
|
|
```sh
|
|
BigButtonIOS.js
|
|
BigButtonAndroid.js
|
|
```
|
|
|
|
But React Native provides two alternatives to easily organize your code separating it by platform:
|
|
|
|
## Platform specific extensions
|
|
React Native will detect when a file has a `.ios.` or `.android.` extension and load the right file for each platform when requiring them from other components.
|
|
|
|
For example, you can have these files in your project:
|
|
|
|
```sh
|
|
BigButton.ios.js
|
|
BigButton.android.js
|
|
```
|
|
|
|
With this setup, you can just require the files from a different component without paying attention to the platform in which the app will run.
|
|
|
|
```javascript
|
|
import BigButton from './components/BigButton';
|
|
```
|
|
|
|
React Native will import the correct component for the running platform.
|
|
|
|
## Platform module
|
|
A module is provided by React Native to detect what is the platform in which the app is running. This piece of functionality can be useful when only small parts of a component are platform specific.
|
|
|
|
```javascript
|
|
var { Platform } = React;
|
|
|
|
var styles = StyleSheet.create({
|
|
height: (Platform.OS === 'ios') ? 200 : 100,
|
|
});
|
|
```
|
|
|
|
`Platform.OS` will be `ios` when running in iOS and `android` when running in an Android device or simulator.
|
|
|
|
###Detecting Android version
|
|
On Android, the Platform module can be also used to detect which is the version of the Android Platform in which the app is running
|
|
|
|
```javascript
|
|
var {Platform} = React;
|
|
|
|
if(Platform.Version === 21){
|
|
console.log('Running on Lollipop!');
|
|
}
|
|
```
|