Eğitim / Kodlama 7 Mayıs 2024

How to Use React Developer Tools – Explained With Examples

How to Use React Developer Tools – Explained With Examples

Traditional browser developer tools are designed to inspect and debug web pages by interacting with your HTML, CSS, and JavaScript code. However, you can’t use them to inspect and debug React applications efficiently due to the nature of React.

This is where the React Developer Tools, AKA React DevTools comes into play. It allows you to inspect and debug your React applications by providing access to the components, states, hooks, props, what renders what, and more.

This article will show you how to use React DevTools by focusing on components, props, and state inspection. We’ll also examine how to use it to enhance application performance.

For demonstration, we’ll use the 2048 game code. You can grab it in this GitHub repository.

Installing the React developer tools extension for your browser is the most common way to use it.

If you use Chrome, visit the Chrome Webstore and search for “React”, then select “React Developer Tools” and click the “Add to Chrome” button to install it.

How to Use React Developer Tools – Explained With Examples
React Devtools in Chrome Web Store

React DevTools is also available as a standalone electron app, an NPM package, and an add-on for both Edge and Firefox browsers. If you use the Safari browser, consider using the NPM package.

If you use the extension on Chrome but want to migrate your data to either Edge and Firefox, it will be automatically installed for you!

After installing the DevTools as an extension, open your browser console and you should see two additional tabs – Components and Profiler.

How to Use React Developer Tools – Explained With Examples
Google Chrome with React DevTools extension

The Components tab displays a tree view of the components in your application. It also gives you access to the hooks and props in each component.

How to Use React Developer Tools – Explained With Examples
Components tab in React DevTools

The Profiler tab allows you to analyze the runtime performance of your applications and identify costly re-renders or performance bottlenecks. From there, you can import and export recorded performance sections and see how long a component renders or why it re-renders.

How to Use React Developer Tools – Explained With Examples
Profiler tab in React DevTools

If you click the gear icon on the right side in any of the tabs, you should see a pop-up with 4 tabs – General, Debugging, Components, and Profiler.

How to Use React Developer Tools – Explained With Examples
React DevTools settings

In the 4 tabs, you can adjust settings related to theme, display, debugging options, component filters, and recording settings for the profiler.

In the Components tab, you can select a component and inspect it, just like you’d do with HTML elements in a traditional browser’s dev tools.

To do that, click the select icon on the top-left corner, then select any part of the app to see the component that represents it:

How to Use React Developer Tools – Explained With Examples
Inspecting components in React DevTools

As you make changes that involve adding something, new components will be added to the tree.

How to Use React Developer Tools – Explained With Examples
Live component updates in React DevTools

On the right-hand side in the Components tab are the props, hooks, renderer, and the source for any component you select in the tree.

How to Use React Developer Tools – Explained With Examples
Component details in React DevTools

How to Explore Component State and Props

Remember that when you select a component in the tree, the state and props in that component are available on the right-hand side.

In the screenshot below, I have selected a Tile component for you to see the props:

How to Use React Developer Tools – Explained With Examples
Tile component selected

You can see that the Tile component has id, position, and value properties. From here, you can add a new prop and edit the existing props.

For example, I just changed a value prop from 2 to 4 and it reflected in the UI in real-time:

How to Use React Developer Tools – Explained With Examples
change value from 2 to 4

You can also make changes to a piece of state. For instance, the scorecard you can see in the UI is a piece of state in the GameProvider context. It updates your score as you play the game.

You can select the GameProvider context, look for the score state, and change it to something else:

How to Use React Developer Tools – Explained With Examples
Change score state

The traditional browser developer tools is good for debugging your HTML, CSS, and JavaScript code, but it has limitations concerning debugging React applications.

This is the primary reason React DevTools was created in the first place, as it has the essential built-in features that can show you everything you need to debug your React apps.

One of the errors the browser can help you debug is a reference error. For instance, if there’s an error in any of your components, the browser can show you the error message and the line affected.

For example, I’ve forced an error by changing cells to cell on line 62 in the board.tsx file of the app. This is what the error message looks like in the browser:

How to Use React Developer Tools – Explained With Examples
Reference Error in React 19

You can then go to the line of code where the error occurred and make the necessary adjustments so the app can run again.

React DevTools also shows you a reference error and the component in which it occurs.

How to Use React Developer Tools – Explained With Examples
Reference Error in React DevTools

In the Components tab, above the items on the right-hand side, are iconized buttons you can use to:

  • Force the selected component to an errored state
  • Inspect the matching DOM element
  • Suspend the selected component
  • Log the component data to the console
  • View the source code for the elements in the selected component
How to Use React Developer Tools – Explained With Examples
Component utilities in React DevtTools

For example, if the tiles in the game are not showing as they should, it’s probably a styling issue.

How to Use React Developer Tools – Explained With Examples
Unstyled tiles in 2048 Game

This particular error will not stop your app from running, so the best way to debug it is to use React DevTools instead of looking through your code, especially if it’s large.

You can select the Tile component and log its data to the console. When you do this, the props, hooks, and nodes of that component will be logged for you.

How to Use React Developer Tools – Explained With Examples
Utility allowing to log component data to the console in React DevTools

To see the component’s data, you have to switch to the console tab and inspect the nodes to see what might have gone wrong.

How to Use React Developer Tools – Explained With Examples
Component data printed in the console React DevTools

In the image above,it shows that there is a div element with an undefined className. This tells you that you’ve misspelt a className value, so you need to go back to your code and correct it.

If the error you’re getting has to do with props or hooks, then you need to open up any of them to see what went wrong.

You can also look through the source code of any component causing an issue. For instance, if the scoreboard is not showing as it has to, select the component and click the “View source code for this element” icon.

How to Use React Developer Tools – Explained With Examples
View source of this component utility in React DevTools

After the source code is displayed, you can step through the end of each line. Any line end that is red when you do that is the one causing the error.

How to Use React Developer Tools – Explained With Examples

You can then go back to your editor and make the necessary changes on that line.

Doing performance analysis with React can help you understand the efficiency of your application and identify any performance bottlenecks. This is what the Profiler tab lets you do.

To do this, switch to the Profiler tab and click the “Start profiling” icon to start recording.

How to Use React Developer Tools – Explained With Examples
Record button in the Profile tab of React DevTools

Perform the actions in your app you want to analyze. This could be page loads, user interactions like button clicks, swiping, or dynamic content loading. When you’re done, click “Stop” to end the recording.

The Profiler will then display a flame graph and a list of commits. Each commit represents a render phase of your React application. Components that take longer to render will have wider bars.

How to Use React Developer Tools – Explained With Examples

You can then select a specific commit to view detailed information about the render performance of components during that commit:

How to Use React Developer Tools – Explained With Examples
Profiling results in React DevTools

You can also download the profiling session, or import a profiling session. This means you can share the session with your teammates.

If DevTools fails to show up, it could be because it doesn’t have access to the React sites you’re viewing in the browser.

To fix this, type chrome://extensions/ into the address bar and hit enter, then search for the extension and make sure it’s toggled on.

How to Use React Developer Tools – Explained With Examples
How to enable React DevTools extension in Google Chrome

If that doesn’t fix the issue, click the “Details” button and make sure you grant an “On all sites” access under the “Site access” option

How to Use React Developer Tools – Explained With Examples
How to change extension’s site access settings in Google Chrome

Conclusion

From inspecting the component hierarchy and modifying state and props to profiling performance and understanding complex re-renders, React Developer Tools offers a comprehensive set of features that can improve the quality of your React applications.

Whether you’re a beginner aiming to have a better understanding of the inner workings of React, or an experienced developer looking to optimize your applications, investing the time to master React Developer Tools will be profitable in your development processes.

Learn React and Next JS

Are you ready to dive deep into React and start creating real-world applications? Enroll in my React and Next JS course on Udemy! You’ll learn through hands-on coding as we build an incredible 2048 game from scratch with cool animations.

Join now and start your journey to becoming an employable React developer!

How to Use React Developer Tools – Explained With Examples
Click to get started

source

Spread the love <3

You may also like...

Nis
27
2024
0
Globalization vocabulary quiz

Globalization vocabulary quiz

You can do this quiz online or print it on paper. It tests some of the terms covered on the...

Spread the love <3
Eyl
18
2024
0

İsrail saldırısından sonra herkesin aklında o soru: Telefonlar uzaktan patlatılabilir mi?

İsrail, tarihte benzeri görülmemiş bir siber saldırı düzenleyerek Hizbullah üyelerine karşı büyük bir darbe indirdi. Bu saldırıda, Hizbullah üyelerinin kullandığı...

Spread the love <3
Eyl
04
2024
0

Intel Core Ultra 200V serisi tanıtıldı!

Intel, gerçekleştirdiği lansmanda tanıttığı Intel Core Ultra 200V serisi işlemciler ile mobil işlemci dünyasında yeni bir sayfa açtı. Bu seri...

Spread the love <3
Mar
08
2024
10
Intel, Amerikan hükümetiyle anlaştı!

Intel, Amerikan hükümetiyle anlaştı!

Ortaya çıkan son bilgilere göre, Intel ile Amerika yönetimi arasında büyük bir çip anlaşması imzalandı. Buna göre, şirket Amerikan ordusunun...

Spread the love <3
Whatsapp İletişim
Merhaba,
Size nasıl yardımcı olabilirim ?