繁体   English   中英

npm 模块在 react.js 项目和 react native 项目中使用:如何在代码中定义不同的行为?

[英]npm module to use both in a react.js project and in a react native project: how to define the different behaviour in the code?

假设在模块的 index.js 中工作,我应该定义类似的东西

if (we are in a react.js project){
   do this;
} else if (we are in react native project){
   do this;
}

如何确定模块是在 reactjs 项目中还是在 react native 项目中使用?

看到这个答案。 它说你可以这样做:

if (typeof document != 'undefined') {
  // React
}
else if (typeof navigator != 'undefined' && navigator.product == 'ReactNative') {
  // React Native
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM