8.0 KiB
id | title | layout | category | permalink | next | previous |
---|---|---|---|---|---|---|
debugging | Debugging | docs | Guides | docs/debugging.html | accessibility | platform-specific-code |
Accessing the In-App Developer Menu
You can access the developer menu by shaking your device or by selecting "Shake Gesture" inside the Hardware menu in the iOS Simulator. You can also use the Command
⌘
+ D
keyboard shortcut when your app is running in the iPhone Simulator, or Command
⌘
+ M
when running in an Android emulator.
The Developer Menu is disabled in release (production) builds.
Reloading JavaScript
Instead of recompiling your app every time you make a change, you can reload your app's JavaScript code instantly. To do so, select "Reload" from the Developer Menu. You can also press Command
⌘
+ R
in the iOS Simulator, or press R
twice on Android emulators.
If the
Command
⌘
+R
keyboard shortcut does not seem to reload the iOS Simulator, go to the Hardware menu, select Keyboard, and make sure that "Connect Hardware Keyboard" is checked.
Automatic reloading
You can speed up your development times by having your app reload automatically any time your code changes. Automatic reloading can be enabled by selecting "Enable Live Reload" from the Developer Menu.
You may even go a step further and keep your app running as new versions of your files are injected into the JavaScript bundle automatically by enabling Hot Reloading from the Developer Menu. This will allow you to persist the app's state through reloads.
There are some instances where hot reloading cannot be implemented perfectly. If you run into any issues, use a full reload to reset your app.
You will need to rebuild your app for changes to take effect in certain situations:
- You have added new resources to your native app's bundle, such as an image in
Images.xcassets
on iOS or theres/drawable
folder on Android. - You have modified native code (Objective-C/Swift on iOS or Java/C++ on Android).
In-app Errors and Warnings
Errors and warnings are displayed inside your app in development builds.
Errors
In-app errors are displayed in a full screen alert with a red background inside your app. This screen is known as a RedBox. You can use console.error()
to manually trigger one.
Warnings
Warnings will be displayed on screen with a yellow background. These alerts are known as YellowBoxes. Click on the alerts to show more information or to dismiss them.
As with a RedBox, you can use console.warn()
to trigger a YellowBox.
YellowBoxes can be disabled during development by using console.disableYellowBox = true;
. Specific warnings can be ignored programmatically by setting an array of prefixes that should be ignored: console.ignoredYellowBox = ['Warning: ...'];
.
In CI/Xcode, YellowBoxes can also be disabled by setting the IS_TESTING
environment variable.
RedBoxes and YellowBoxes are automatically disabled in release (production) builds.
Accessing console logs
You can display the console logs for an iOS or Android app by using the following commands in a terminal while the app is running:
$ react-native log-ios
$ react-native log-android
You may also access these through Debug → Open System Log...
in the iOS Simulator or by running adb logcat *:S ReactNative:V ReactNativeJS:V
in a terminal while an Android app is running on a device or emulator.
Chrome Developer Tools
To debug the JavaScript code in Chrome, select "Debug JS Remotely" from the Developer Menu. This will open a new tab at http://localhost:8081/debugger-ui.
Select Tools → Developer Tools
from the Chrome Menu to open the Developer Tools. You may also access the DevTools using keyboard shortcuts (Command
⌘
+ Option
⌥
+ I
on Mac, Ctrl
+ Shift
+ I
on Windows). You may also want to enable Pause On Caught Exceptions for a better debugging experience.
It is currently not possible to use the "React" tab in the Chrome Developer Tools to inspect app widgets. You can use Nuclide's "React Native Inspector" as a workaround.
Debugging on a device with Chrome Developer Tools
On iOS devices, open the file RCTWebSocketExecutor.m
and change "localhost" to the IP address of your computer, then select "Debug JS Remotely" from the Developer Menu.
On Android 5.0+ devices connected via USB, you can use the adb
command line tool to setup port forwarding from the device to your computer:
adb reverse tcp:8081 tcp:8081
Alternatively, select "Dev Settings" from the Developer Menu, then update the "Debug server host for device" setting to match the IP address of your computer.
If you run into any issues, it may be possible that one of your Chrome extensions is interacting in unexpected ways with the debugger. Try disabling all of your extensions and re-enabling them one-by-one until you find the problematic extension.
Debugging using a custom JavaScript debugger
To use a custom JavaScript debugger in place of Chrome Developer Tools, set the REACT_DEBUGGER
environment variable to a command that will start your custom debugger. You can then select "Debug JS Remotely" from the Developer Menu to start debugging.
The debugger will receive a list of all project roots, separated by a space. For example, if you set REACT_DEBUGGER="node /path/to/launchDebugger.js --port 2345 --type ReactNative"
, then the command node /path/to/launchDebugger.js --port 2345 --type ReactNative /path/to/reactNative/app
will be used to start your debugger.
Custom debugger commands executed this way should be short-lived processes, and they shouldn't produce more than 200 kilobytes of output.
Debugging with Stetho on Android
-
In
android/app/build.gradle
, add these lines in thedependencies
section:compile 'com.facebook.stetho:stetho:1.3.1' compile 'com.facebook.stetho:stetho-okhttp3:1.3.1'
-
In
android/app/src/main/java/com/{yourAppName}/MainApplication.java
, add the following imports:import com.facebook.react.modules.network.ReactCookieJarContainer; import com.facebook.stetho.Stetho; import okhttp3.OkHttpClient; import com.facebook.react.modules.network.OkHttpClientProvider; import com.facebook.stetho.okhttp3.StethoInterceptor; import java.util.concurrent.TimeUnit;
-
In
android/app/src/main/java/com/{yourAppName}/MainApplication.java
add the function:public void onCreate() { super.onCreate(); Stetho.initializeWithDefaults(this); OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(0, TimeUnit.MILLISECONDS) .readTimeout(0, TimeUnit.MILLISECONDS) .writeTimeout(0, TimeUnit.MILLISECONDS) .cookieJar(new ReactCookieJarContainer()) .addNetworkInterceptor(new StethoInterceptor()) .build(); OkHttpClientProvider.replaceOkHttpClient(client); }
-
Run
react-native run-android
-
In a new chrome tab, open :
chrome://inspect
, click on 'Inspect device' (the one followed by "Powered by Stetho")
Debugging native code
When working with native code (e.g. when writing native modules) you can launch the app from Android Studio or Xcode and take advantage of the debugging features (setup breakpoints, etc.) as you would in case of building a standard native app.
Performance Monitor
You can enable a performance overlay to help you debug performance problems by selecting "Perf Monitor" in the Developer Menu.