Skip to content

Latest commit

 

History

History
61 lines (46 loc) · 1.48 KB

platform.md

File metadata and controls

61 lines (46 loc) · 1.48 KB
id platform
title Platform
officialDoc https://reactnative.dev/docs/platform-specific-code

Platform.os

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 === Platform.ios {
  // your code
}

Platform.os can be

  • Platform.ios when running on iOS
  • Platform.android when running on Android.
  • Platform.web when running on the web (via react-native-web).

If you need an unsupported platform, you can use Platform.unsafe(string).

For conditional style depending on the platform, you can do the following:

open Style

let styles = StyleSheet.create({
  "wrapper": s({width: (Platform.os == Platform.ios ? 100. : 200.)->pct}),
})

Platform.select

⚠️ Unsupported.

This feature isn't relevant with ReScript. Instead you can do the following:

open Style

let styles = StyleSheet.create({
  "wrapper": s({
    width: switch Platform.os {
    | os if os == Platform.ios => 100.
    | os if os == Platform.android => 200.
    | os if os == Platform.web => 250.
    | os if os == Platform.unsafe("windows") => 300.
    | _ => 150.
    }->pct,
  }),
})

Also since spreading things in ReScript is not supported (because of the unsafety aspect), it's even less relevant to have this binding.