Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if using nextJS, react or react-native

I'm building a library working with React apps. Since it will be working on the React ecosystem, some features are going to work differently according to the environment.

I'm looking for a way to detect whether the current environment is a ReactJS app (made with create-react-app for example), or if it's using NextJS over it for the SSR, or if it's a mobile app made with React Native.

Does it exist a javascript way to know this information?

Thank you :)

like image 769
Fantasim Avatar asked Jul 21 '26 00:07

Fantasim


1 Answers

To check if your code is running in react native, you can use navigator.product. React Native returns navigator.product as "ReactNative". Here is a link to the proposal which is implemented. https://github.com/facebook/react-native/issues/1331

To check whether your code is running in NextJS or Browser, you could check if window object or document object is defined or not. Under nodeJS, window is undefined.

React native has defined common browser objects like window and document to ensure libraries that target browsers work. Here is a link to the code. https://github.com/facebook/react-native/blob/master/Libraries/Core/InitializeCore.js

like image 119
Amal Ajith Avatar answered Jul 22 '26 15:07

Amal Ajith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!