Skip to content

Commit 722a320

Browse files
committed
[docs] Overhaul "Debugging Basics" guide
1 parent d872c42 commit 722a320

10 files changed

+87
-84
lines changed

docs/debugging.md

Lines changed: 77 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -14,141 +14,135 @@ React Native provides an in-app developer menu which offers several debugging op
1414

1515
Alternatively for Android devices and emulators, you can run `adb shell input keyevent 82` in your terminal.
1616

17-
![](/docs/assets/DevMenu.png)
17+
![The React Native Dev Menu](/docs/assets/debugging-dev-menu.jpg)
1818

1919
:::note
2020
The Dev Menu is disabled in release (production) builds.
2121
:::
2222

23-
## LogBox
24-
25-
Errors and warnings in development builds are displayed in LogBox inside your app.
26-
27-
:::note
28-
LogBox is disabled in release (production) builds.
29-
:::
23+
## Opening the Debugger
3024

31-
### Console Errors and Warnings
32-
33-
Console errors and warnings are displayed as on-screen notifications with a red or yellow badge, and the number of errors or warning in the console respectively. To view a console error or warnings, tap the notification to view the full screen information about the log and to paginate through all of the logs in the console.
34-
35-
These notifications can be hidden using `LogBox.ignoreAllLogs()`. This is useful when giving product demos, for example. Additionally, notifications can be hidden on a per-log basis via `LogBox.ignoreLogs()`. This is useful when there's a noisy warning that cannot be fixed, like those in a third-party dependency.
25+
The debugger allows you to understand and debug how your JavaScript code is running, similar to a web browser.
3626

3727
:::info
38-
Ignore logs as a last resort and create a task to fix any logs that are ignored.
28+
**In Expo projects**, press <kbd>j</kbd> in the CLI to directly open the Hermes Debugger.
3929
:::
4030

41-
```tsx
42-
import {LogBox} from 'react-native';
43-
44-
// Ignore log notification by message:
45-
LogBox.ignoreLogs(['Warning: ...']);
31+
<Tabs groupId="js-debugger" queryString defaultValue={constants.defaultJsDebugger} values={constants.jsDebuggers}>
32+
<TabItem value="hermes">
4633

47-
// Ignore all log notifications:
48-
LogBox.ignoreAllLogs();
49-
```
34+
Hermes supports the Chrome debugger by implementing the Chrome DevTools Protocol. This means Chrome's tools can be used to directly debug JavaScript running on Hermes, on an emulator or on a physical device.
5035

51-
### Unhandled Errors
36+
1. In a Chrome browser window, navigate to `chrome://inspect`.
37+
2. Use the "Configure..." button to add the dev server address (typically `localhost:8081`).
38+
3. You should now see a "Hermes React Native" target with an **"inspect"** link. Click this to open the debugger.
5239

53-
Unhandled JavaScript errors such as `undefined is not a function` will automatically open a full screen LogBox error with the source of the error. These errors are dismissable and minimizable so that you can see the state of your app when these errors occur, but should always be addressed.
40+
![Overview of Chrome's inspect interface and a connected Hermes debugger window](/docs/assets/debugging-hermes-debugger-instructions.jpg)
5441

55-
### Syntax Errors
42+
</TabItem>
43+
<TabItem value="flipper">
5644

57-
When syntax error occurs the full screen LogBox error will automatically open with the stack trace and location of the syntax error. This error is not dismissable because it represents invalid JavaScript execution that must be fixed before continuing with your app. To dismiss these errors, fix the syntax error and either save to automatically dismiss (with Fast Refresh enabled) or <kbd>Cmd ⌘</kbd>/<kbd>Ctrl</kbd> + <kbd>R</kbd> to reload (with Fast Refresh disabled).
45+
[Flipper](https://fbflipper.com/) is a native debugging tool which provides JavaScript debugging capabilities for React Native via an embedded Chrome DevTools panel.
5846

59-
## JavaScript debugging
47+
To debug JavaScript code in Flipper, select **"Open Debugger"** from the Dev Menu. Learn more about Flipper [here](https://fbflipper.com/docs/features/react-native/).
6048

61-
### Flipper
49+
:::info
50+
To debug using Flipper, the Flipper app must be [installed on your system](https://fbflipper.com/docs/getting-started/).
51+
:::
6252

63-
To debug JavaScript code in Flipper, select "Open Debugger" from the Dev Menu. This will automatically open the debugger tab inside Flipper.
53+
![The Flipper desktop app opened to the Hermes debugger panel](/docs/assets/debugging-flipper-console.jpg)
6454

65-
To install and get started with Flipper, follow [this guide](https://fbflipper.com/docs/getting-started/).
55+
:::warning
56+
Debugging React Native apps with Flipper is [deprecated in React Native 0.73](https://github.com/react-native-community/discussions-and-proposals/pull/641). We will eventually remove out-of-the box support for JS debugging via Flipper.
57+
:::
6658

67-
### Expo CLI
59+
</TabItem>
60+
<TabItem value="new-debugger">
6861

69-
If you're using Expo CLI in a project running with Hermes, you can debug your JavaScript code by starting your project with `npx expo start` and then pressing <kbd>j</kbd> to open the debugger in Google Chrome or Microsoft Edge.
62+
:::note
63+
**This is an experimental feature** and several features may not work reliably today. When this feature does launch in future, we intend for it to work more completely than the current debugging methods.
64+
:::
7065

71-
## Chrome Developer Tools
66+
The React Native team is working on a new JavaScript debugger experience, intended to replace Flipper, with a preview available as of React Native 0.73.
7267

73-
:::info
74-
**Starting from version 0.73, Remote Debugging is deprecated.** These Chrome DevTools steps use the _Remote Debugging_ workflow, where JS code is executed in Chrome's V8 engine on the dev machine during debugging, instead of on-device. This method is incompatible with some New Architecture features such as JSI.
68+
The new debugger can be enabled via React Native CLI. This will also enable <kbd>j</kbd> to debug.
7569

76-
Please prefer using Flipper or [direct debugging with Safari](#safari-developer-tools).
77-
:::
70+
```sh
71+
npx react-native start --experimental-debugger
72+
```
7873

79-
<details>
80-
<summary>Re-enabling Remote Debugging in React Native 0.73</summary>
74+
When selecting **"Open Debugger"** in the Dev Menu, this will launch the new debugger using Google Chrome or Microsoft Edge.
8175

82-
If your project still relies on this feature, you can manually enable it manually through the `NativeDevSettings.setIsDebuggingRemotely` function.
76+
![The new debugger frontend opened to the "Welcome" pane](/docs/assets/debugging-debugger-welcome.jpg)
8377

84-
```jsx
85-
import NativeDevSettings from 'react-native/Libraries/NativeModules/specs/NativeDevSettings';
86-
export default function App() {
87-
useEffect(() => {
88-
NativeDevSettings.setIsDebuggingRemotely(true);
89-
}, []);
78+
</TabItem>
79+
</Tabs>
9080

91-
return <MyApp />;
92-
}
93-
```
81+
## React DevTools
9482

95-
</details>
83+
You can use React DevTools to inspect the React element tree, props, and state.
9684

97-
### Debugging on a physical device
85+
```sh
86+
npx react-devtools
87+
```
9888

99-
:::info
100-
If you're using Expo CLI, this is configured for you already.
101-
:::
89+
![A React DevTools window](/docs/assets/debugging-react-devtools-blank.jpg)
10290

103-
<Tabs groupId="platform" defaultValue={constants.defaultPlatform} values={constants.platforms} className="pill-tabs">
104-
<TabItem value="ios">
91+
:::tip
10592

106-
On iOS devices, open the file [`RCTWebSocketExecutor.mm`](https://github.com/facebook/react-native/blob/master/packages/react-native/React/CoreModules/RCTWebSocketExecutor.mm) and change "localhost" to the IP address of your computer, then select "Debug JS Remotely" from the Dev Menu.
93+
**Learn how to use React DevTools!**
10794

108-
</TabItem>
109-
<TabItem value="android">
95+
- [React DevTools guide](/docs/next/react-devtools)
96+
- [React Developer Tools on react.dev](https://react.dev/learn/react-developer-tools)
11097

111-
On Android 5.0+ devices connected via USB, you can use the [`adb` command line tool](https://developer.android.com/tools/help/adb.html) to set up port forwarding from the device to your computer:
98+
:::
11299

113-
```sh
114-
adb reverse tcp:8081 tcp:8081
115-
```
100+
## LogBox
116101

117-
Alternatively, select "Settings" from the Dev Menu, then update the "Debug server host for device" setting to match the IP address of your computer.
102+
Errors and warnings in development builds are displayed in LogBox inside your app.
118103

119-
</TabItem>
120-
</Tabs>
104+
![A LogBox warning and an expanded LogBox syntax error](/docs/assets/debugging-logbox.jpg)
121105

122106
:::note
123-
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.
107+
LogBox is disabled in release (production) builds.
124108
:::
125109

126-
<details>
127-
<summary>Advanced: Debugging using a custom JavaScript debugger</summary>
110+
### Console Errors and Warnings
128111

129-
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 "Open Debugger" from the Dev Menu to start debugging.
112+
Console errors and warnings are displayed as on-screen notifications with a red or yellow badge, and a notification count. To see more about an error or warning, tap the notification to see an expanded view and to paginate through other logs.
130113

131-
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.
114+
LogBox notifications can be disabled using `LogBox.ignoreAllLogs()`. This can be useful when giving product demos, for example. Additionally, notifications can be disabled on a per-log basis via `LogBox.ignoreLogs()`. This can be useful for noisy warnings or those that cannot be fixed, e.g. in a third-party dependency.
132115

133-
:::note
134-
Custom debugger commands executed this way should be short-lived processes, and they shouldn't produce more than 200 kilobytes of output.
116+
:::info
117+
Ignore logs as a last resort and create a task to fix any logs that are ignored.
135118
:::
136119

137-
</details>
120+
```js
121+
import {LogBox} from 'react-native';
138122

139-
## Safari Developer Tools
123+
// Ignore log notification by message
124+
LogBox.ignoreLogs([
125+
// Exact message
126+
'Warning: componentWillReceiveProps has been renamed',
140127

141-
You can use Safari to debug the iOS version of your app when using JSC.
128+
// Substring or regex match
129+
/GraphQL error: .*/,
130+
]);
142131

143-
- On a physical device go to: `Settings → Safari → Advanced → Make sure "Web Inspector" is turned on` (This step is not needed on the Simulator)
144-
- On your Mac enable Develop menu in Safari: `Settings... (or Preferences...) → Advanced → Select "Show Develop menu in menu bar"`
145-
- Select your app's JSContext: `Develop → Simulator (or other device) → JSContext`
146-
- Safari's Web Inspector should open which has a Console and a Debugger
132+
// Ignore all log notifications
133+
LogBox.ignoreAllLogs();
134+
```
147135

148-
While sourcemaps may not be enabled by default, you can follow [this guide](https://blog.nparashuram.com/2019/10/debugging-react-native-ios-apps-with.html) or [video](https://www.youtube.com/watch?v=GrGqIIz51k4) to enable them and set break points at the right places in the source code.
136+
### Syntax Errors
149137

150-
However, every time the app is reloaded (using live reload, or by manually reloading), a new JSContext is created. Choosing "Automatically Show Web Inspectors for JSContexts" saves you from having to select the latest JSContext manually.
138+
When a JavaScript syntax error occurs, LogBox will open with the location of the error. In this state, LogBox is not dismissable since your code cannot be executed. LogBox will automatically dismiss once the syntax error is fixed — either via Fast Refresh or after a manual reload.
151139

152140
## Performance Monitor
153141

154-
You can enable a performance overlay to help you debug performance problems by selecting "Perf Monitor" in the Dev Menu.
142+
On Android and iOS, an in-app performance overlay can be toggled during development by selecting **"Perf Monitor"** in the Dev Menu. Learn more about this feature [here](/docs/performance).
143+
144+
![The Performance Monitor overlay on iOS and Android](/docs/assets/debugging-performance-monitor.jpg)
145+
146+
:::info
147+
The Performance Monitor runs in-app and is a guide. We recommend investigating the native tooling under Android Studio and Xcode for accurate performance measurements.
148+
:::

docs/react-devtools.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: react-devtools
3-
title: React Developer Tools
3+
title: React DevTools
44
---
55

66
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import constants from '@site/core/TabsConstants';

website/core/TabsConstants.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ const snackLanguages = [
4444
];
4545
const defaultSnackLanguage = 'typescript';
4646

47+
const jsDebuggers = [
48+
{label: 'Hermes Debugger / Expo', value: 'hermes'},
49+
{label: 'Flipper', value: 'flipper'},
50+
{label: 'New Debugger (Experimental)', value: 'new-debugger'},
51+
];
52+
const defaultJsDebugger = 'flipper';
53+
4754
const guides = [
4855
{label: 'Expo Go Quickstart', value: 'quickstart'},
4956
{label: 'React Native CLI Quickstart', value: 'native'},
@@ -73,6 +80,7 @@ const getDevNotesTabs = (tabs = ['android', 'ios', 'web', 'windows']) =>
7380

7481
export default {
7582
defaultGuide,
83+
defaultJsDebugger,
7684
defaultOs,
7785
defaultPackageManager,
7886
defaultPlatform,
@@ -87,6 +95,7 @@ export default {
8795
syntax,
8896
androidLanguages,
8997
javaScriptSpecLanguages,
98+
jsDebuggers,
9099
snackLanguages,
91100
defaultSnackLanguage,
92101
};
81.5 KB
Loading
89.7 KB
Loading
88.8 KB
Loading
109 KB
Loading
98.7 KB
Loading
85 KB
Loading
85.5 KB
Loading

0 commit comments

Comments
 (0)