If you follow these steps you can use VS Code to debug the JavaScript for your react-native-windows app. This is a better experience than debugging in the Chrome debugger.
Installation
- Launch VS Code in your app directory
- Install the React Native Tools plugin. See: https://www.youtube.com/watch?v=_srHOd6EFQ0
Enable Web Debugging
Before you can start debugging, you need to enable your application to use Web Debugging. You can do this by customizing your React application instance settings in both C++ and C#. In C++,
- In your application root directory, navigate to
windows/<your application name>/MainPage.cpp
- Find the instance settings declaration
InstanceSettings settings;
in theMainPage::LoadReact()
method - Set the
UseWebDebugger
property to true;settings.UseWebDebugger = true;
.
In C#:
- In your application root directory, navigate to
windows/<your application name>/MainPage.xaml.cs
- Find the instance settings declaration
InstanceSettings settings;
in theLoadReact()
method - Set the
UseWebDebugger
property to true;settings.UseWebDebugger = true;
.
Configuration
Hit the Debug button, notice the Debug toolbar has "No Configurations". Hit the drop down arrow and select "Add Configuration…". This will add a new file: .vscode\launch.json
. You will need to edit this file and in order to add your debug configuration.
There are two scenarios in which you can debug a React Native Windows Application with VS Code:
- "Launch": In this scenario, VS Code launches the application itself and breaks into it in one action. This is the easiest scenario because it requires the least amount of steps.
- "Attach to Packager" scenario: In this scenario, the developer has to start the React Native packager, attach the VS Code debugger to the packager and then start the application. This scenario is more involved than the "Launch" scenario.
We will now look at how we can configure VS Code to debug using either scenario:
"Launch" scenario
Once you open launch.json press the "Add Configuration..." button. From the presets pick "React Native: Debug Windows".
"Attach to Packager" scenario
Once you open launch.json press the "Add Configuration..." button. From the presets pick "React Native: Attach to packager".
Launch and connect to the packager
The steps to launch the application vary based on the two scenarios:
"Launch" scenario
"Attach to Packager" scenario
- From your command-prompt, launch the packager from your app directory by running
react-native start
. - In VS Code, select "Attach to Packager" from the Debug drop-down and hit the green arrow to launch it.
- Launch your app. It should connect to the debugger running inside VS Code
If all is successful you'll see this logged in the Debug Console:
Starting debugger app worker.
Established a connection with the Proxy (Packager) to the React Native application
You can now set breakpoints in your JavaScript inside VS Code and use the VS Code debugger 🎉!