-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add static platform check props #37870
Conversation
Base commit: c870a52 |
isAndroid: true, | ||
isIOS: false, | ||
isMacOS: false, | ||
isNative: true, | ||
isWeb: false, | ||
isWindows: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be awesome!
But, this is kind of "redundant" since:
- Android and iOS will use and read from their respective files.
- If the platform is Android this would implicitly mean it's not iOS, etc., and vice-versa?
- Why would an Android app would need a check for MacOS,
native, web, and Win similarly for iOS app? - If we want to support (check) all these platforms then we should create another generic file (interface with "all props optional") that will define/expose them.
WDYT?
P.S.: I may be unaware of the full context here :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is certainly redundant. I've just noticed that many RN projects have the same const isAndroid = Platform.OS === 'android'
declared and thought there may as well be a utility for devs to express one off platform checks this way offered directly by RN.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current design here is that OS
is a string constant, defined per platform. This allows platforms to work out-of-tree, public or private, by defining their own constant. So, the mechanism you use to check for iOS/Android is the same whether you are developing on a phone, on desktop, or a PlayStation. This is something we lose with this change.
It also means we need to update this centralized list, and if a platform loses support, we need to either break API, or keep around a dead interface.
Some of that has leaked into the TypeScript typings in this repo that used to be on DefinitelyTyped, but out-of-tree platforms like React Native Windows have more recently gained general ability to re-export their own types, and tell tsc
to resolve types for a specific platform during typechecking.
@motiz88 pointed out offline that Metro has Babel transforms which use Platform.OS
conditions in the AST to strip code for platforms not being bundled for. So switching to these helpers would defeat the dead code elimination.
@NickGerleman Good to know. What do you think about something like |
My main question here is "why?" What does this achieve from a user's perspective that I don't mean to sound harsh. It's just that, considering the extra effort required to optimise and then teach this proposed new API, I'm afraid such an API wouldn't be all that valuable. (If anything we should be looking at ways to deprecate and then remove the existing I think we'd certainly be open to a new API if one is warranted, but we should probably discuss the motivation and design in an RFC before going any further with an implementation. |
In terms of functionality it doesn't accomplish anything new. And yet as stated above almost every RN project I've seen has something along the lines of
I'm curious about these optimizations. I was unaware that there was a babel transform stripping dead code based on
Fair enough. |
Can confirm, having |
Opened an RFC. |
I'm not sure this is documented, but here is a link to where So, a condition might be transformed to a constant comparison like I tend to agree though, that adding |
This PR is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days. |
This PR was closed because it has been stalled for 7 days with no activity. |
Summary:
I've noticed that a lot of RN projects have something along the lines of the following in them:
This seems common enough that I think it makes sense to just export these from RN itself. Platform already has
isPad
andisTV
. This would add another way to express platform checks with that API.Changelog:
[GENERAL] [ADDED] - Adds isAndroid, isIOS, isMacOS, isNative, isWeb, and isWindows static properties to Platform
Test Plan:
I just patched these into a demo app. Here's a screenshot: