react-native/docs/PlatformSpecificInformation.md
Héctor Ramos deb6106c16 Add a new Handling Touches guide
Summary:
The new Handling Touches guide provides an overall view of how touches can be handled. It is meant to be a higher level discussion of basic touch handling, e.g. "how do I implement a button?". The existing Gesture Responder System guide has been moved to the end of the docs and is still available for reference when building custom gesture handlers.

Reference: #8160

![handlingtouchesguide](https://cloud.githubusercontent.com/assets/165856/16256634/50a20c92-3808-11e6-8a5b-b49f2cda9fca.png)
Closes https://github.com/facebook/react-native/pull/8299

Differential Revision: D3469681

Pulled By: JoelMarcey

fbshipit-source-id: 3bc18e759b26c2d5c141b626acb433c5e973cef0
2016-06-22 08:58:37 -07:00

98 lines
2.9 KiB
Markdown

---
id: platform-specific-code
title: Platform Specific Code
layout: docs
category: Guides
permalink: docs/platform-specific-code.html
next: gesture-responder-system
---
When building a cross-platform app, you'll want to re-use as much code as possible. Scenarios may arise where it makes sense for the code to be different, for example you may want to implement separate visual components for iOS and Android.
React Native provides two ways to easily organize your code and separate it by platform:
* Using the `Platform` module.
* Using platform-specific file extensions.
```javascript
import BigButton from './components/BigButton';
```
React Native will import the correct component for the running platform.
## Platform module
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.
```javascript
import { Platform, StyleSheet } from 'react-native';
const styles = StyleSheet.create({
height: (Platform.OS === 'ios') ? 200 : 100,
});
```
`Platform.OS` will be `ios` when running on iOS and `android` when running on Android.
There is also a `Platform.select` method available, that given an object containing Platform.OS as keys, returns the value for the platform you are currently running on.
```javascript
import { Platform, StyleSheet } from 'react-native';
const styles = StyleSheet.create({
container: {
flex: 1,
...Platform.select({
ios: {
backgroundColor: 'red',
},
android: {
backgroundColor: 'blue',
},
}),
},
});
```
This will result in a container having `flex: 1` on both platforms, a red background color on iOS, and a blue background color on Android.
Since it accepts `any` value, you can also use it to return platform specific component, like below:
```javascript
const Component = Platform.select({
ios: () => require('ComponentIOS'),
android: () => require('ComponentAndroid'),
})();
<Component />;
```
### Detecting the Android version
On Android, the `Platform` module can also be used to detect the version of the Android Platform in which the app is running:
```javascript
import { Platform } from 'react-native';
if(Platform.Version === 21){
console.log('Running on Lollipop!');
}
```
## Platform-specific extensions
When your platform-specific code is more complex, you should consider splitting the code out into separate files. React Native will detect when a file has a `.ios.` or `.android.` extension and load the relevant platform file when required from other components.
For example, say you have the following files in your project:
```sh
BigButton.ios.js
BigButton.android.js
```
You can then require the component as follows:
```javascript
const BigButton = require('./BigButton');
```
React Native will automatically pick up the right file based on the running platform.