mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-06-02 10:35:54 +00:00
[add] Platform.select
This commit is contained in:
+28
-11
@@ -10,19 +10,36 @@ specific.
|
|||||||
|
|
||||||
`Platform.OS` will be `web` when running in a Web browser.
|
`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
|
```js
|
||||||
|
import { Platform } from 'react-native';
|
||||||
|
|
||||||
const styles = StyleSheet.create({
|
const styles = StyleSheet.create({
|
||||||
height: (Platform.OS === 'web') ? 200 : 100,
|
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 = {
|
const Platform = {
|
||||||
OS: 'web',
|
OS: 'web',
|
||||||
userAgent: canUseDOM ? window.navigator.userAgent : ''
|
select: (obj: Object) => obj.web
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Platform
|
module.exports = Platform
|
||||||
|
|||||||
Reference in New Issue
Block a user