124

I'd like to add some debug-only UI to my React Native app, but I can't find any equivalent of RCT_DEBUG or RCT_DEV compile-time flags in the JavaScript environment. Is there one?

Use case: I want to add a status bar that shows the number of HTTP requests initiated by my app. Obviously this is not part of a shipping app, but it would help me check my work while in development and testing.

escouten
  • 2,883
  • 2
  • 21
  • 23

1 Answers1

255
if (__DEV__) {
    console.log('I am in debug');
}

You can see this approach is being used in React Native repository.

Jalal
  • 6,594
  • 9
  • 63
  • 100
rmevans9
  • 5,472
  • 1
  • 21
  • 18
  • 1
    is this still valid? if so source please? – zianwar Aug 30 '16 at 16:53
  • 1
    @zianwar https://github.com/facebook/react-native/tree/master/packager#pathtomodulenamemapbundle-query-params – Jameal G Dec 11 '16 at 15:38
  • @JamealG "dev boolean, defaults to true: sets a global __DEV__ variable which will effect how the React Native core libraries behave." is the line you're looking for. Good find! – Joshua Pinter Apr 18 '17 at 13:29
  • 7
    or just shake it once :P – Manjeet Singh Aug 28 '17 at 21:03
  • it seems to be valid and is used in [AppContainer.js](https://github.com/facebook/react-native/blob/1151c096dab17e5d9a6ac05b61aacecd4305f3db/Libraries/ReactNative/AppContainer.js#L55) – Top-Master Apr 25 '19 at 07:10
  • see also [Can react-native bundler detect unused files?](https://stackoverflow.com/questions/55844101/can-react-native-bundler-detect-unused-files) – Top-Master Apr 25 '19 at 07:39
  • 2
    does not work with debug apk, debug apk builds with __dev__ as false – Tal C Sep 04 '20 at 21:11
  • The problem is, i still cannot run console.table when __DEV__ is true, how do i find out whether the react native is in debug mode? – Hadi KAR Nov 26 '20 at 02:49
  • This doesn't work well for me on React Native 0.63 and Android. `__DEV__` is `true in `staging` builds as well. Maybe `release` as well but I didn't test that. – Joshua Pinter Jun 19 '21 at 12:58