React Native Debugging: Techniques, Tools, How to Use it?
Updated on Oct 25, 2024 | 5 min read | 23.4k views
Share:
For working professionals
For fresh graduates
More
Updated on Oct 25, 2024 | 5 min read | 23.4k views
Share:
Table of Contents
Debugging in the React Native universe may be done in various ways and with various tools, as React Native is built up of distinct environments (iOS and Android), which means there are a variety of problems to solve with tools to use. React Native Debugging entails a detailed examination of the code and aids in the early detection of errors.
Fixing these problems or defects during the React Native app development process rather than during the production stages will save money for any React Native app development company. Thanks to the vast number of contributors to the React Native ecosystem, many debugging tools are available.
React native debugging is essential for developing apps. It is a process of identifying, detecting and fixing bugs or issues. A thorough understanding of debugging techniques like Chrome developer tools or React Native Debugger enables more rapid and efficient software development.
We will go through a couple of debugging approaches in React Native here. We will be developing, running, and debugging our apps with expo-cli, one of the simplest and fastest ways to construct a React Native app.
The following are some common react native debugging techniques:
It is a simple and quick way to debug your software while still in development. It is one of the most straightforward methods for learning how an application works. To log, we just utilize the console.log() instructions to record the appropriate data or indicators. However, we must never forget to remove these console.log() statements before submitting our product to the development phase, as they will only add overhead.
We may activate Remote Debugging in our application, which will allow us to debug React Native code in a Chrome browser tab, like how we debug web applications with Chrome developer tools.
We can utilize React Native Tools, a VS Code addon used for debugging React Native projects. Simply create a new file named Launch.json after installing the extension, which will be used to debug the configuration file construction. Finally, we must enable Debug JS Remotely as well as Live Reload. We can now begin debugging with VS Code.
Professional online programmes like the Full Stack Software Development Bootcamp from upGrad offer extensive knowledge about tools like the React debugger to develop error-free programs and applications.
Here are the features of React Native Debugger
You must be aware of element inspector if you are a web developer. The UI Inspector in React Native Debugger works similarly: one can see all the tags used in your app and inspect their styling. You can use the inspector to test your UI and adjust the styling.
This is, by far, the most efficient approach to viewing and debugging UI hierarchy in React Native projects.
You will need a state management library whether you are working on a React or React Native app. Redux is one of the most popular state management libraries on the market.
However, especially in React Native, using Redux might be a nuisance if you cannot debug your state. You may use React Native Debugger to debug your Redux-based project easily. You may even debug the current condition in real-time.
Until we specifically If we do not use redux devtools in our project, it will not work properly. As seen in the code snippet below, first must enable redux in our app by inserting certain config lines to our App.js file either to our redux main function, to use redux devtools in the react-native-debugger:
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(reducers, /* preloadedState, */ composeEnhancers(middlewares));
If we now reload our RN Debugger tool and perform some activities, we will see the following in the debugger console:
As you will see, the React Native Debugger exposes the entire Redux state, so users do not need to use breakpoints or console logs to figure out how data flow in the redux system. One incredible feature is the ability to go back in time directly from the debugger to reverse and re-run Redux activities. Another notable feature is the ability to see the exact difference between two state transitions, which is especially useful for projects with many Redux states.
In general, we may not know what is happening behind the scenes while designing an app that needs to connect to a server. When submitting a request to the server, for example, it may be difficult to track down an issue. Developers typically debug this by printing or alerting error messages; however, this is time-consuming (even though most people do it), as it takes many iterations to comprehend the condition fully.
We can easily monitor every inbound and outbound traffic request between the app and the server using the React Native Debugger.
You can easily print or log your AsyncStorage on the console by running the following command:
console.log(showAsyncStorageContentInDev())
This will print or report the AsyncStorage data in the terminal on React Native Debugger.
The React Native Debugger also allows us to pause the execution job anytime. This will allow developers to inspect data states or understand app activity anytime during the app's lifecycle.
Check Out upGrad’s Software Development Courses to upskill yourself.
Step 1: Run your application in the iOS simulator.
Step 2: Press Command + D and a webpage should open at http://localhost:8081/debugger-ui. (Chrome only for now) or use the Shake Gesture
Step 3: Enable Pause On Caught Exceptions for a better debugging experience.
Step 4: Press Command + Option + I to open the Chrome Developer tools or open it via View -> Developer -> Developer Tools.
Step 5: You should now be able to debug as you normally would.
In the iOS Simulator, React Native supports a few keyboard shortcuts. They are outlined below. To make them work,
==> Go to the Hardware menu
==> Pick Keyboard
==> And check "Connect Hardware Keyboard."
Shake your device or pick "Shake Gesture" from the Hardware menu in the iOS Simulator to get to the developer menu. When your app runs in the iOS Simulator, or M when executing in an Android emulator on Mac OS, or Ctrl+M on Windows and Linux, developers can use the D keyboard shortcut. To open the dev menu on an Android device, use the command adb shell input keyevent 82. (82 being the Menu key code).
Fast Refresh is a React Native feature that provides near-instant feedback for changes made to your React components. Enabling Fast Refresh when debugging can be beneficial. Fast Refresh is enabled by default, and you may enable it in the React Native developer menu by toggling "Enable Fast Refresh." Most of your adjustments should be displayed within a second or two once you enable it.
In development builds, errors and warnings are presented inside your app.
Errors
Inside your app, errors are displayed in a full-screen alert with a red background. A RedBox is the name for this type of display. You can manually trigger one using console.error().
Warnings
Warnings will have a yellow background on the screen. YellowBoxes are the name for these notifications. To get more information or dismiss the notifications, click on them.
A YellowBox can be triggered using console.warn(), much like a RedBox.
YellowBoxes can be turned off in the console during development.
disableYellowBox=true; By setting an array of prefixes to ignore, certain warnings can be ignored programmatically:
import { YellowBox } from ‘react-native’;
YelloBox.ignoreWarnings([‘Warning:….’[);
Select "Debug JS Remotely" from the Developer Menu in Chrome to debug the JavaScript code. http://localhost:8081/debugger-ui will open in a new tab.
To open the Developer Tools, go to the Chrome Menu and select Tools Developer Tools. You can also use keyboard shortcuts to get to the DevTools (I on macOS, Ctrl Shift I on Windows). For a better react native debugging experience, you might also wish to set Pause On Caught Exceptions.
One of the drawbacks of utilizing Chrome's DevTools with React Native is that you cannot debug styles or change their properties as you can with web apps. When investigating React's component hierarchy, it is likewise constrained compared to React's devtools.
You do not need to enable "Debug JS Remotely" to debug the iOS version of your app with Safari.
You must use the desktop app to debug React Native using React's Developer Tools. Simply use the following command to install it globally or locally in your project:
yarn add react-devtools
or npm:
npm install react-devtools --save
After that, run yarn react-devtools to activate the app.
For these two reasons, React's Developer Tools may be the greatest tool for debugging React Native:
Except for layout, basic state, and characteristics:
Although it works with emulated and real devices and is cross-platform, it is a highly specialized debugger. If I needed to debug my layout or determine where a component was getting props wrong, I would use this.
The redeeming grace is that it also sets the highlighted component as a variable in Chrome Inspector, allowing you to dump the property's state there. It is quite a clever trick.
This article teaches us about the React Native debugging, Debugger tool, its installation method, how it works, use-cases, and essential capabilities. It will undoubtedly make any React Native developer's life easier, more productive, and more efficient. Additionally, your apps will be faster and have fewer issues.
We strongly advise you to use the React Native Debugger instead of the Chrome Dev Tools because it contains a set of key capabilities that will significantly speed up your work.
You can reinforce your resume as a full-stack developer by pursuing courses like Full Stack Software Development Bootcamp and AI Powered FSD Course by IIITB. These courses impart the in-demand full-stack development skills that help you explore outstanding job opportunities like full-stack developer, frontend developer, backend developer, and UI developer.
Boost your software engineering skills with our top programs. Explore the courses below to find the right fit
Master in-demand software skills with our specialized courses. Explore your options below to upskill today
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources