Debugging is one of the crucial activity during the software development. It refers to the process of identifying an error in a software application that causes performance issues, then analyzing the issue, and finally resolving it. Debugging allows you to validate your code and fix issues before pushing a software application into the production stage. However, debugging issues are likely to arise during various phases – development, testing, and even production/post-deployment. And, implementing the right debugging tools and methodologies will speed up development and enhance the efficiency of the end-product.
Coming to React Native debugging; the framework is made up of different environments, the most prominent being Android and iOS. As a result, it becomes challenging to debug apps due to multiple platforms involved. Moreover, React Native offers a huge variety of tools for debugging which seems confusing for many, particularly newbies. This post guides you through some effective practices and useful React Native and React debugger tools that will help you to identify issues and fix them like an expert.
React Native Debugging Methodologies and Best Practices
Identifying and Addressing Console Errors, Warnings, and Logs
Console errors and warnings are visible in the form of on-screen notifications with a red or yellow badge.
Errors will be displayed inside a RedBox with the command console.error(). This RedBox contains a description of the error and also a suggestion on how to resolve the error. Take a look at this example. If you write a style property that the React Native framework doesn’t support, or if you write a property meant to be used for a particular element (like a backroundImage needs to be set for the element View), then the RedBox shows an error along with the list of supported style properties that can be applied to the View.
Warnings will be displayed inside a YellowBox with the command console.warn()Such warnings include information on performance-related issues and deprecated code. Most of these warnings indicate some bad practice within your code. Examples of warnings include a notification about the presence of an eventListener that isn’t being removed, the presence of deprecated features and dependencies, etc.
For issuing logs, either use the command react-native log-android or use the Chrome console with the command console.log(string str)
React Native developers can hide the notifications concerning errors and warnings with the command LogBox.ignoreAllLogs() whenever needed, like during product demos. You can also hide the notifications on the basis of per-log with the command LogBox.ignoreLogs(). This method works in scenarios when a noisy warning cannot be resolved like the ones in a third-party dependency. But, whenever you ignore logs, make sure to create a task for fixing those ignored logs later.
Debugging using React Native’s built-in Debug Mode
Check out how to utilize the built-in debug mode offered by the React Native eco-system employing browsers like Safari or Chrome.
Debugging with Chrome
Install these react-devtools for supporting React Native: Yarn and NPM. Then use the development mode to open the in-app developer menu; here you start the debugging process by activating the debug option.
From the Developer Menu, select the option Debug JS Remotely. This action opens up a channel to a JS debugger. A new tab opens; here, from the Chrome menu, choose Tools – Developer Tools; for opening the devtools.
The other most prominent options to utilize in the in-app developer menu to debug apps are:
Enable Live Reload: for automatically reloading the app, Enable Hot Reloading: for identifying the modifications that result in a changed file, and Start Systrace starts the Android marker-based profiling tool. The option Toggle Inspector helps in toggling an inspector interface. This way, developers can inspect UI elements present on the screen and examine their properties. Then an interface is presented; this interface contains other tabs such as networking for displaying the HTTP calls as well as a performance-related tab. The option Show Perf Monitor tracks your app’s performance.
Debugging with Safari
Your app’s iOS version can be debugged using Safari, and here, you do not have to enable “Debug JS Remotely”. Simply open Preferences and then select the following options:
- Show Develop menu in the menu bar
Next, choose the JSContext of your application:
Develop – Simulator – JSContext
Now, the Web Inspector of Safari is going to open and you would be able to view a Debugger and a Console. And, each time you reload the app, either manually or by using the fast refresh (live reload), a new JSContext will get created. Remember to select the option Automatically Show Web Inspectors for JSContexts; otherwise, the latest JSContext will be selected manually.
Prominent React/React Native Debugger Tools
This set of React tools is employed for debugging the component hierarchy of React and works well for front-end operations. Using this toolset, you can view what’s there deep within your component tree – select the options state of the component and edit the current props.
React Developer Tools are the extension to the browsers Firefox and Chrome. However, to debug apps built in React Native, you need to use an autonomous version of React DevTools and run this command on your terminal – npm install –g react-devtools. Then launch the app by running the command react-devtools; select the option Show Inspector from the in-app developer menu for examining the app’s UI elements.
If Redux is used, then you need to use React DevTools along with ReduxDev Tools to fully understand your component’s state. For this, Redux DevTools need to be separately installed and here, the tool React Native Debugger proves quite useful as well.
React Native Debugger
The React Native Debugger is immensely beneficial if Redux is used during React Native app development. This is a standalone tool that functions on Linux, Windows, and macOS. This tool is used for logging or deleting Async Storage content, detecting as well as diagnosing performance issues, and examining network requests.
The best part is that it integrates React DevTools as well as Redux DevTools within a single app and so, there’s no.