As an application grows in size, developers often find themselves tracking down and fixing pesky bugs. This process can be made significantly less cumbersome by utilizing different programs (called debuggers) and methodologies. These bugs are not always the result of poorly implemented code; they can arise due to the inherent complexity of the tasks at hand.
React offers a range of features and options to facilitate the debugging process, making it straightforward and simple. In this article, we will explore the tools and techniques for debugging React Native apps, helping developers effectively debug applications.
Debugging, at its core, is the art of pinpointing and correcting flaws in code, a fundamental aspect of software development. It involves the use of debugging tools and methods. By following proper methods and employing the right tools, you can save time and reduce effort.
Bugs are inevitable, especially as an app grows in size; you're likely to encounter them. For React Native developers, there are multiple methods and tools available to help address these bugs. Now, let's explore the tools and methods used by React Native developers.
React Native comes equipped with an in-app debugging tool, which includes a variety of debugging options to help developers tackle bugs and resolve issues. You can enable different methods or options through the React Native Dev Menu.
You can access it by either shaking your device or using keyboard shortcuts:
Alternatively, for Android devices and emulators, you can run adb shell input keyevent 82 in your terminal.
As you can see, React Native provides debugger and React DevTools to streamline and enhance the debugging process.
Additionally, it includes features including Reload and Show Inspector to make it easier further.
React shipped LogBox with version 0.63 to handle errors and warnings. It is a modified version of the initial RedBox and YellowBox, with more concise, well-formatted, and actionable logging output.
The Red LogBox displays errors in three sections:
Yellow LogBox displays warnings in two sections:
You can enable LogBox by importing React Native.
require('react-native').unstable_enableLogBox();
LogBox Functions:
LogBox addresses concerns about errors and warnings being overly verbose, poorly formatted, or lacking actionable information. It shows warnings and errors as notifications. When you tap the notification, you can view the full-screen information about the log.
Code snippet to display warning and error manually:
console.warn(‘this is my warning’)
console.error(‘this is my error)
You can also hide these notifications when giving product demos by using LogBox.ignoreAllLogs()
import {LogBox} from 'react-native';
// Ignore log notification by message:
LogBox.ignoreLogs(['Warning: ...']);
//Ignore all log notifications:
LogBox.ignoreAllLogs();
Within the React Native Dev Menu, you can open the debugger, which typically takes you to http://localhost:8081/debugger-ui.
Chrome DevTools is a popular way for debugging web applications, including React Native apps. Chrome DevTools offers a range of features, including the capacity to establish breakpoints, inspect elements, and monitor network requests. With Chrome DevTools, you can examine the component hierarchy, establish breakpoints, and step through your code line by line.
Here are some of the actions you can perform with Chrome DevTools when debugging React Native apps:
For React Native iOS apps, you can also use Safari to debug your iOS version of React Native application and use workflows like setting breakpoints, inspecting variables, etc.
On your Mac, let's get Safari all set up for debugging:
Now you can debug your app.
To debug React Native JavaScript code in Safari, you need to enable inline sourcemap, as the JSC debugger in Safari does not use the network stack. Enabling inline sourcemap makes it easier to debug JavaScript code. Once you enable it, Safari can recognize the sourcemaps and open the source files.
This can be done by adding a single line to your AppDelegate.m file:
[NSURL URLWithString:[[[[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil] absoluteString] stringByAppendingString:@"&inlineSourceMap=true" ]];
The React Native Debugger is a standalone debugger for React Native apps. It offers several features that are not found in Chrome DevTools, including the capability to debug asynchronous code and inspect the state of React components. You can integrate with Chrome DevTools, and You will get two new tabs in your Chrome DevTools: "⚛️ Components" and "⚛️ Profiler".
To begin the debugging process with React Developers Tools, you must install React Native Debugger first.
You can do so by installing it from GitHub. Alternatively, if you are using macOS, you can execute this command in your terminal:
brew update && brew install --cask react-native-debugger
Once you've downloaded and installed the package, you can start using it. To use it, run your app and activate the debug mode by either shaking your mobile device
Or
pressing Command + Shift + Z (macOS)
or
Ctrl + M (Windows)
and selecting the debug option. For developers’ ease, it includes UI Inspector, Debug Redux, Network Inspector, AsyncStorage management, Breakpoints, and more.
Reactotron is an open-source application for debugging React Native applications. It features a rich interface for debugging your app, which includes the ability to inspect Redux or MobX-State-Tree application state, view custom logs, run custom commands such as resetting state, store and restore state snapshots, and other helpful debugging features for React Native apps.
Additionally, you can install it on Expo. Kindly refer to this article detailing how to install it.
There are a few things to keep in mind when debugging React Native apps on iOS and Android:
To debug your React Native app on Android, you can use the adb command to set up port forwarding from your device to your computer. This will allow you to use the Chrome Developer Tools to debug your app.
adb reverse tcp:8081 tcp:8081
Alternatively, select "Settings" from the Dev Menu, then update the "Debug server host for device" setting to match the IP address of your computer.
To debug your React Native app on iOS, you need to change the localhost variable in the RCTWebSocketExecutor.mm file to the IP address of your computer. Then, you can select "Debug JS Remotely" from the Developer Menu to start debugging your app.
There are a number of advanced debugging techniques that developers can use for React Native apps. Some of these techniques include:
We hope you find this article useful. In this article, we have discussed some React Native debugging tools and techniques that will help you debug your application and save you time.
At Brilworks, we provide React Native development services. If you need React Native development services to develop, modernize, or fix bugs in your application, whether you require a dedicated company or are looking to hire a React Native developer, connect with us today to get started.
You might also like
Get In Touch
Contact us for your software development requirements