我怎么知道我的代码是否以React Native的身份运行

问题描述:

我希望能够为所有平台导出软件包,但是我使用了一些带有纯JS后备功能的本机绑定.通常我会注意到差异检查对象 window exports 是否存在.

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.

如何在React Native上实现这一目标?

How can I achieve this on React Native?

这里是如何检查代码是否在Web,nodejs或react-native上:

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
}

来源: