mirror of
https://github.com/zhigang1992/react-native-web.git
synced 2026-04-23 12:07:26 +08:00
[add] Platform.select
This commit is contained in:
@@ -10,19 +10,36 @@ specific.
|
||||
|
||||
`Platform.OS` will be `web` when running in a Web browser.
|
||||
|
||||
**userAgent**: string
|
||||
|
||||
On Web, the `Platform` module can be also be used to detect the browser
|
||||
`userAgent`.
|
||||
|
||||
## Examples
|
||||
|
||||
```js
|
||||
import { Platform } from 'react-native';
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
height: (Platform.OS === 'web') ? 200 : 100,
|
||||
});
|
||||
|
||||
if (Platform.userAgent.includes('Android')) {
|
||||
console.log('Running on Android!');
|
||||
}
|
||||
```
|
||||
|
||||
## Methods
|
||||
|
||||
**select**: any
|
||||
|
||||
`Platform.select` takes an object containing `Platform.OS` as keys and returns
|
||||
the value for the platform you are currently running on.
|
||||
|
||||
```js
|
||||
import { Platform } from 'react-native';
|
||||
|
||||
const containerStyles = {
|
||||
flex: 1,
|
||||
...Platform.select({
|
||||
android: {
|
||||
backgroundColor: 'blue'
|
||||
},
|
||||
ios: {
|
||||
backgroundColor: 'red'
|
||||
},
|
||||
web: {
|
||||
backgroundColor: 'green'
|
||||
}
|
||||
})
|
||||
});
|
||||
```
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment'
|
||||
|
||||
const Platform = {
|
||||
OS: 'web',
|
||||
userAgent: canUseDOM ? window.navigator.userAgent : ''
|
||||
select: (obj: Object) => obj.web
|
||||
}
|
||||
|
||||
module.exports = Platform
|
||||
|
||||
Reference in New Issue
Block a user