我怎么知道我的代码是否以React Native的身份运行 [英] How do I know if my code is running as React Native

查看:39
本文介绍了我怎么知道我的代码是否以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
}

来源:

这篇关于我怎么知道我的代码是否以React Native的身份运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆