title | author | date | |
---|---|---|---|
Platform isAndroid, isIOS, and isNative properties |
|
2023-06-14 |
Add isAndroid
, isIOS
, and isNative
to Platform
.
if (Platform.isAndroid) {
// do some Android only stuff here
}
Platform already defines isPad
, isTV
, and isTesting
. In consulting and the companies I've worked at I've seen dozens (even hundreds) of apps, and nearly all backfill the missing ones isAndroid
and isIOS
with something along the lines of the following:
const isAndroid = Platform.OS === "android";
const isIOS = Platform.OS === "ios";
Platform
should provide these since so many devs want to express one-off platform checks this way.
I did an initial implementation here but it has some drawbacks. As @NickGerleman pointed out we don't want to maintain a centralized list of platforms. A better solution that keeps the platform definitions in their respective platforms is to update Platform
to be a proxy object.
const Platform = new Proxy(
{
__constants: null,
OS: "android",
isAndroid: true,
isNative: true,
// ...
},
{
get(target, prop) {
if (prop in target) {
return target[prop];
}
if (prop.toString().startsWith("is")) {
return false;
}
return Reflect.get(...arguments);
},
}
);
- This change doesn't really offer any new functionality over conditional logic using
Platform.OS
. It's just a different (albeit quite common) way of expressing the same thing. - As @motiz88 points out this breaks existing optimizations with Babel transforms that remove dead code based on
Platform.OS
conditionals. This proposal likely further complicates that by makingPlatform
a proxy object. - All other platforms would need to update their
Platform
implementations to match.
Another potential solution that doesn't use proxy but reads similarly would be to add an is
method (e.g. Platform.is('android')
or Platform.is('ios')
). This may or may not have advantages considering the Babel transforms. It is less similar to the common code that inspired this proposal though.
This is not a breaking change but would be a new API offering the same existing functionality provided by checking against Platform.OS
. Existing React Native developers would upgrade to latest and discover via docs and types.
Per motivation, React Native developers are already doing this. This just moves the definition of isAndroid
and isIOS
up into React Native.
- Where are the Babel transforms for removing platform specific code and how do they work?