Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know if my code is running as React Native

I want to be able to export a package for all platforms, but I am using some native bindings with a plain JS fallback. Normally I would notice the difference checking if object window or exports exist.

How can I achieve this on React Native?

like image 425
jsdario Avatar asked Sep 13 '16 10:09

jsdario


People also ask

How do you know if a project is react or React Native?

So when you write your components, you can check if React. View exists. If it does, you're using React Native. Otherwise, you're using the web version.

How do you know if an app is made with React Native?

Go to res->layout and search for com_facebook... , In my case it was com_facebook_activity_layout. xml . If you have one layout with com_facebook in it then this apk was created with React.


1 Answers

Here is how to check if code is on web, nodejs, or react-native:

if (typeof document !== 'undefined') {
  // I'm on the web!
}
else if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
  // I'm in react-native
}
else {
  // I'm in node js
}

Sources:

  • What causes node to return 'navigator is not defined'
  • Add way to detect react-native (and platform) without require().
  • React Native support is broken
like image 129
BradByte Avatar answered Oct 23 '22 19:10

BradByte