How to Debug in React
Tags: react, javascript, beginners, codenewbie
Published: 2021-02-16 03:37:52

When I started learning React, it looked completely foreign. I couldn't recognize where the JavaScript was and that made debugging feel foreign too.

One thing that will make debugging feel easier, though, is knowing that we can still use Vanilla JavaScript in React! React is just a library (which is written in JavaScript!) that gives us access to different functions and extensions (like JSX). We have to follow some new rules which can feel tricky at first, but will make certain aspects of debugging a lot easier. Knowing how to diagnose an issue while learning a new coding language can make things seem more manageable.  

One great new tool we have access to is the React Dev Tools. This is a browser extension that allows you to inspect your components in React. This becomes helpful when you want to inspect the state or props in a component. For example, props and state was very confusing when I first started learning React. I would pass some props down the same way I saw in class, but wasn't really sure if it was working. 

Fear not! Even if you don't understand what props are, like I didn't, you can take baby steps and just make sure you are passing props down correctly by inspecting a components props and state.

![Viewing State](https://dev-to-uploads.s3.amazonaws.com/i/f7t979m9fvgkecd49e2z.png)

![Viewing Props](https://dev-to-uploads.s3.amazonaws.com/i/up27rhk26a6rp8kikwmu.png)

Seeing props and state laid out like this also helps us visualize what props and state are. For example, we passed some props down from our App component to the RecipeContainer. Even though those recipes are a part of App's state, once they are passed down to the RecipeContainer, they do not become part of RecipeContainer's state. They are now props in RecipeContainer.

Seeing props like this also becomes useful when we want a child component to have different functionality based on two different parent components. Take this Flatiron Stocks lab for example. Whenever we click on a Stock component that belongs to the StockContainer, we want to add it to our PortfolioContainer. When we click on that same Stock component in the PortfolioContainer, we want that stock to be removed from our portfolio. 

![Stocks Example](https://dev-to-uploads.s3.amazonaws.com/i/umxoqe299rs99qa0z4ft.gif)

Unless we create buttons, we can only put one click action on our stock card.

A work-around would be for the PortfolioContainer and the StockContainer to pass down a remove and add function, respectively, to the Stock component. However, when we pass this function down, we give the function the same prop name. So now, depending on where this prop came from, it has different functionality in our Recipe component. 

![StockContainer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36b49tab5uiv60watg5p.png)

![PortfolioContainer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6n3ywpjc4tmx3n57hq9.png)

![Stock.js](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1jdmjxe0w9vs01to9wz7.png)

This might be hard to grasp just looking at our code, but when we look at our Dev Tools, we can see all of the individual Recipe components laid out, each with a different function, based on its parent.

![DevTools clickAction](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oq27jyxp73h0ejm9557e.gif)

We can also use `console.log`s to debug! As I said earlier, we get to still use JavaScript code in React. The difference comes in how we write our `console.log`s and where. First of all, a React component always has to render valid JSX. For now, what JSX exactly is doesn't matter, but if you see code that looks like HTML in your `.js` or `.jsx` files, know that you have to wrap any Javascript code in curly braces. 

![console.log with curly braces](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7s6tvj0spsh4osxcbyz2.png)

If you are writing Javascript outside of your return statement, no curly braces are needed! It's just like regular JavaScript.

![console.log with no curly braces](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i475likzdrvblcaqrkdz.png)

Lastly, we can still use debuggers.

Since a lot of what happens in React works asynchronously, we might write out some code that in theory should run, but we just don't see anything happening on our screen. Adding a debugger helps you pause your code and you might see something happen that you weren't seeing before. If that happens, you'll know it's a timing issue, which can be solved by adding something like a setTimeout function to your code that needs to wait for another event to fire off first.


*Without debugger (Example from a Flatiron lab)*
![error without debugger](https://p69.f3.n0.cdn.getcloudapp.com/items/eDu8j7GR/fd8d38fd-45bf-4eac-b185-56c8dfb75a17.gif?source=viewer&v=419cd852dcabc2c0b00b6177c6bbbed9)


*With debugger*
![add a debugger](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k5wjumyopheajx2tu081.png)

![error with debugger](https://p69.f3.n0.cdn.getcloudapp.com/items/OAugP2xw/bc579e28-8926-4e98-a69a-7a391ba658fb.gif?v=8d44e8f0d879f63b09580db18cc9e66b)


*Fixing the issue*
![adding a setTimeout](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jhee6od4m2mc4mkw41x1.png)

![Working app!](https://p69.f3.n0.cdn.getcloudapp.com/items/YEuXRDBq/5aaaac83-fb05-450c-9dc4-231ac2a7aa8e.gif?v=6b0387b2a7ace84e20fa16671daa034c)

These are just the basics of debugging, and, of course, you'll run into more complex issues that require some more research. Knowing how to diagnose what might be going wrong in the first place is a good place to start, though.

Happy Debugging!