How to debug a React App
Tags: javascript, programming, react, webdev
Published: 2022-06-27 05:49:19

## Debug React apps like a pro!

While working as a front-end developer for last 4+ years, one thing I have witnessed junior developers constantly struggle with is proper debugging skills. I believe debugging is a skill and developers who are good at it can bring immense value. As with any other skill, one can learn debugging. All one needs to understand the tools at one’s disposal to debug React app.

This little handy guide is not only about debugging React apps, but any JavaScript based app. Some principles here are generic to programming while the others are targeted towards debugging JavaScript applications.

## Re-read the error messages

![Old lady reading meme](https://miro.medium.com/max/1040/1*vbSlIJHd0aC923hKcHM1nA.jpeg)

Old lady reading meme

Debugging is no rocket science. Sometimes developers just panic and what could have been resolved within 5 minutes, take 30. I say this because I have spent 30 minutes.

The maintainers of programming language, library and frameworks have worked hard to make error messages self explanatory. Therefore, when you debug, the very first step is to read the error message and then re-read it.

I want to lay emphasis on **re-reading** the error message because usually the solution is screaming at you. Skimming through the error message is not what you should do. Read it. Slowly. Understand it and then move forward.

The error message lays out the entire [call-stack](https://www.freecodecamp.org/news/understanding-the-javascript-call-stack-861e41ae61d4/). How the error bubbled up and at which line number the error was thrown, everything is out there. Going through the error call-stack will help you analyze the code flow and warnings. Reading and making sense of the error call-stack is an essential skill a developer must work on as it can help one work with legacy code-bases which are an amalgamation of hundreds of files and thousands of lines of code!

For example, if we have a `button` element which calls a function `showAlert` but we forget to define such a function, the app will throw an error.

React is kind enough to show the error at two places:

## **The dev-tools**

The console of the dev-tools of all the browsers will show this error.

![React throwing error in dev-tools console](https://miro.medium.com/max/1260/1*cATMSKixR9z0p0UDjFuJ7Q.png)

_React throwing error in dev-tools console_

## **The web page**

React ensures that the error is more human readable on the web-page. You can clearly see below where the error was thrown and hence this will be the starting point to debug React app.

![React error message on the web-page](https://miro.medium.com/max/1188/1*cH7cAEyX9m6VSlxxyQFzJg.png)

_React error message on the web-page_

I cannot stress enough on how important it is to read and then re-read the error message. You need to understand it completely before you even start exploring Stack Overflow and Google.

One thing that I have experienced is that Google is pretty good at its job as a search engine. It will give you back the results for the question you asked. Now if you have mis-understood the error and are trying to ask Google wrong questions, it will give you answers which will take you down the rabbit hole of copy-pasting different solutions until something works!

## Learn to use your IDE

![Queen Elizabeth II insists on cutting cake with a sword at G7 event](https://miro.medium.com/max/1260/1*Mj2_i7eXnacPTuCXQwXuZg.png)

_Queen Elizabeth II insists on cutting cake with a sword at G7 event_

Today, Integrated Development Environments (IDE) are very powerful. They hint you what’s wrong right in your code, even before you **Save** it. Not understanding the tools you have slows you down and makes you inefficient at your job. Understanding how your IDE works is so important that programmers and content creators out there have created full fledged courses on the very topic. Some of my favourites around VS Code are:

1.  [Visual Studio Code Tutorial for Beginners — Introduction](https://www.youtube.com/watch?v=VqCgcpAypFQ)
2.  [Visual Studio Code Crash Course](https://www.youtube.com/watch?v=WPqXP_kLzpo)

You can use VS Code extensions/plugins for effective debugging as well. For JavaScript, plugins like [ESLint by Microsoft](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) are a must. If you want your project to ship with an ES Lint, then you can do that as well by using a [NPM library](https://www.digitalocean.com/community/tutorials/linting-and-formatting-with-eslint-in-vs-code).

If you find yourself relying too much on plugins and external NPM libraries, please remember this:

> **To a man with a hammer, everything looks like a nail.**
> 
> – **Mark Twain**

React apps with proper ESLinting and IDE, will even fail to compile, eventually throwing the error in the terminal:

![React app failing to compile due to undefined variable](https://miro.medium.com/max/1013/0*w9YNsXmMCAMekcO8.png)

_React app failing to compile due to undefined variable_

## Working with Breakpoints

This is one of the most helpful and yet one of the most under-utilized piece of debugging tool developers use. In software development, a breakpoint is an intentional stopping or pausing place in a program which is added to debug the code. One can work with breakpoints in multiple ways, but the two most prominent are:

1.  Breakpoints via Dev Tools in the browser
2.  Using the debugger in the IDE like VSCode

## Breakpoints via Dev Tools in the browser

Open the Chrome dev tools. I am using Google Chrome as my primary browser, but dev tools are present in all browsers. Go to `Sources`. Go to the `Page` tab and the `localhost:3000` dropdown. There you’ll see all your files and you can add debuggers to your JS files right then and there.

![Location of all the files of your app in dev tools](https://miro.medium.com/max/792/0*1wKu1ZkfERevFfxt.png)

_Location of all the files of your app in dev tools_

Here, I’ll be adding a breakpoint on the `testDebugger` function, just before it consoles the value of the variable `a`. As soon as you click the button, it will stop the execution of the app on the very breakpoint. You can then use the panel of buttons on the top-right corner (in Red box) to move around the function.

For a more detailed tutorial, you can refer [Debugging JavaScript — Chrome DevTools 101](https://www.youtube.com/watch?v=H0XScE08hy8).

If you look closely below, you’ll be able to see all the breakpoints, the call stack and the file on the left panel.

![Debugger in Chrome Dev Tools to debug React app](https://miro.medium.com/max/1260/0*n8D4R7XtYDLcTHQ-.png)

_Debugger in Chrome Dev Tools to debug React app_

## Using the debugger in the IDE like VSCode

Today the IDEs come with in-built debuggers. You do not have to rely on the dev tools in the browser. The workings of the VSCode debugger is similar to debugger in dev tools. To understand it, you can refer the following two resources:

1.  [The New Way To Debug JavaScript in VS Code — No Extension Required](https://www.youtube.com/watch?v=tC91t9OvVHA)
2.  [How to Use a Debugger — Debugger Tutorial](https://www.youtube.com/watch?v=7qZBwhSlfOo)

This is how the debugger will look like in the VSCode:

1.  Yellow: Debugger
2.  Blue: The breakpoint
3.  Pink: List of all breakpoints
4.  Red: Variables

![VSCode Debugger in action](https://miro.medium.com/max/1260/0*FE2H3e7F4JBUMbwr.png)

_VSCode Debugger in action_

## React Developer Tools

[React Developer Tools](https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en) is a browser extension that helps to debug React apps. It is present in other browsers as well, but I’ll recommend you to get comfortable with the Chrome extension. This extension makes the process to debug React app easy and consistent!

From the official docs:

> React Developer Tools is a Chrome DevTools extension for the open-source React JavaScript library. It allows you to inspect the React component hierarchies in the Chrome Developer Tools.
> 
> – [React Developer Tools (Chrome Web Store)](https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en)

Once you enable this extension, you’ll see two new additions to your dev tools:

1.  Components
2.  Profiler

The Components will show only React components and not the entire DOM like the `Elements` tab shows.

![Demo of React Dev Tools chrome extension](https://miro.medium.com/max/1260/0*heFAsPmMMfDUcAFJ.png)

_Demo of React Dev Tools chrome extension_

You can learn here with examples:

1.  [Intro to React Dev Tools](https://www.youtube.com/watch?v=rb1GWqCJid4)
2.  [React Developer Tools | Components & Profiler](https://www.youtube.com/watch?v=4U37IRrt_zQ)

## Reach out for help!

![Baby Yoda Meme](https://miro.medium.com/max/1260/0*DA3o0puX2q1S0Zib.jpeg)

_Baby Yoda Meme_

Sometimes the error message is cryptic or we cannot make sense of it. Even after struggling to find a working solution, trying everything from Stack Overflow and doubting your entire life, you aren’t able to debug. In such cases, pause. Take a deep breath and ask for help!

If you are a junior developer working in a company, then reach out to the senior developers. Sometimes all you need is a fresh perspective to the whole situation. If you are a self-taught developer and are building something all by yourself (kudos to you!), reach out to the very welcoming developer community on Twitter and Discord. The community, combined with [this one skill](https://adityatyagi.com/index.php/2021/05/15/one-must-have-skill-as-a-software-engineer/) can help you debug any bug!

Few developers know, the programming community on Discord is amazing! All you have to do is ask the relevant question in the relevant channel and you’ll get the solution. If not, the solution, there will be 1 kind soul to help you move forward with your quest to debug React app.

Some of the Discord communities I follow:

1.  [Reactiflux](https://discord.gg/rTnFySNq)
2.  [Academind](https://academind.com/community)
3.  [Blue Collar Coder](https://discord.com/invite/EVsW3mj)

If you are still stuck, call it a day. Sleep on it and tackle it with a fresh mind the next day. You’ll be surprised how often the answers are just in front of us.

Thank you for your time. I hope I was able to bring value.

I’m reachable on some social media channels, so consider checking me on:

-   [**Twitter**](https://twitter.com/secondbestcoder) especially if you’re into programming and software development in general.
-   [**LinkedIn**](https://www.linkedin.com/in/aditya-tyagi-03b4189a/) if you want to get in touch for some collaboration opportunity.