Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (157).NET Core  (28).NET MAUI  (36)Angular  (65)ASP.NET  (48)ASP.NET Core  (74)ASP.NET MVC  (88)Azure  (31)Blazor  (134)DocIO  (22)Essential JS 2  (76)Essential Studio  (169)File Formats  (25)Flutter  (121)JavaScript  (167)Microsoft  (109)PDF  (64)React  (48)Succinctly series  (126)Syncfusion  (552)TypeScript  (30)Uno Platform  (3)UWP  (4)Vue  (34)Webinar  (30)Windows Forms  (57)WinUI  (40)WPF  (131)Xamarin  (148)XlsIO  (25)Other CategoriesBarcode  (5)BI  (29)Bold BI  (4)Build conference  (6)Business intelligence  (53)Button  (4)C#  (115)Chart  (56)Cloud  (11)Company  (445)Dashboard  (7)Data Science  (3)Data Validation  (6)DataGrid  (53)Development  (335)Doc  (7)DockingManager  (1)eBook  (95)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (22)Extensions  (17)File Manager  (4)Gantt  (11)Gauge  (9)Git  (4)Grid  (27)HTML  (9)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (277)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (28)Performance  (9)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (7)Road Map  (10)Scheduler  (34)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (21)Solution Services  (2)Spreadsheet  (11)SQL  (8)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (11)Tips and Tricks  (88)UI  (190)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (1)Visual Studio  (23)Visual Studio Code  (14)Web  (304)What's new  (179)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Share on twitter
Share on facebook
Share on linkedin
Improve React Custom Hook Debugging with useDebugValue

Improve React Custom Hook Debugging with useDebugValue

As developers, we often need to write complex code to fulfill requirements. However, there’s always a chance that our code may run into errors that prevent it from working as expected.

Traditionally, we use debuggers with IDEs or browser dev tools to debug apps, but when it comes to custom React Hooks, these debugging methods are not very effective. Therefore, we need a different way of debugging custom React Hooks.

This article addresses how to debug custom React Hooks with the useDebugValue Hook.

What is useDebugValue?

useDebugValue is a React Hook that allows developers to effectively debug their custom Hooks by assigning custom formatted labels to them. Developers can observe these labels with the help of React Dev Tools, which provide insight into the internal logic of the custom Hook to understand its process.

Defining a custom Hook

Before using the useDebugValue Hook, let us first try debugging a custom Hook without anything else.

To do so, let us define a custom Hook (useFetch) that fetches data from a given endpoint.

The following code example explains the custom Hook.

import { useCallback, useEffect, useState } from "react"; 

export const useFetch = (url: string) => { 
  const [response, setResponse] = useState<any>(); 
  const [error, setError] = useState<any>();

  const getData = useCallback(async () => { 
    try { 
     const resp = await fetch(url, { 
       method: 'GET', 
       mode: 'cors', 
       headers: { 
            'Content-Type': 'application/json' 
       }, 
     }); 

     const data = await resp.json(); 
     setResponse(data); 
     setStatus(data.success); 
  } catch (err) { 
      setError(err); 
    } 
 }, [url]);

 useEffect(() => { 
  getData(); 
 }, [getData, url]);

 return { response, error };
}

This example illustrates the custom React Hook that fetches data from a given endpoint. It has two state variables responsible for holding the response and the error returned from the API. Finally, it returns a JavaScript object to the consumer.

Further, we will consume this Hook in the App component to obtain COVID-19 statistics using the COVID-19 API. The following code example elaborates on the use of the custom useFetch Hook.

import React from 'react';
import logo from './logo.svg';
import './App.css';
import { useFetch } from './use-fetch'; 

function App() { 
  const fetchedResponse =   useFetch('https://www.hpb.health.gov.lk/api/get-current-statistical'); 
  
  return ( 
   <div className="App"> 
     <header className="App-header"> 
       <img src={logo} className="App-logo" alt="logo" /> 
       <p> Edit 
         <code>src/App.tsx</code> 
          and save to reload. 
       </p> 
       <a className="App-link" 
          href="https://reactjs.org" 
          target="_blank"  
          rel="noopener noreferrer" > 
          Learn React 
      </a> 
    </header> 
  </div> 
 );
} 

export default App;

When this Hook is observed in React Dev Tools, the state variables and their flow during the Hook’s execution are shown.

Defining a custom Hook

In addition, there are a few other issues in monitoring Hooks with React DevTools:

  • The content is not readable. To analyze the data variable (output provided by the Hook), we have to break the object tree.
  • The content is not understandable at first glance. The Hook and its output data don’t have a label to provide meaningful information.
  • The Fetch Hook has no description on it. This makes it hard to understand its purpose.
  • React Dev Tools only provide meaningful information on the state variables used by the Hook, not the code written.

This is where useDebugValue comes into play. With its help, we can improve the visibility and overall understandability of the code executed within the Hook by providing custom formatted labels.

Using the useDebugValue Hook

Step 1: Importing the Hook

You can easily import the useDebugValue Hook from React. Refer to the following code.

import { useDebugValue } from "react";

Step 2: Calling the Hook

We can’t invoke the useDebugValue Hook inside another React Hook except within a custom Hook.

To start, observe the response state variable by calling the Hook initially and providing the formatting to it so that it is easily interpreted by anyone observing the logs for our custom Hook in React Dev Tools. To do so, invoke the Hook using useDebugValue(). The Hook requires an object of any type to be passed as the first parameter.  Therefore, the response state variable is passed with a message.

Refer to the following code example.

import { useCallback, useDebugValue, useEffect, useState } from "react"; 

export const useFetch = (url: string) => { 
  const [response, setResponse] = useState<any>(); 
  const [status, setStatus] = useState<boolean>(false); 
  const [error, setError] = useState<any>();

  useDebugValue('Success State of API Call: ' + status); 
  useDebugValue('Error State of API Call: ' + error); 
  useDebugValue('Data Returned From API Call: ' + response);

  const getData = useCallback(async () => { 
    try { 
     const resp = await fetch(url, { 
       method: 'GET', 
       mode: 'cors', 
       headers: { 
         'Content-Type': 'application/json' 
       }, 
    }); 
    const data = await resp.json(); 
    setResponse(data); 
    setStatus(data.success); 
   } catch (err) { 
      setError(err); 
     } 
  }, [url]);

  useEffect(() => { 
    getData(); 
  }, [getData, url]);

  return { response, error };
}

We see a new state variable, status, introduced when observing the updated Hook. This is because React does not allow useDebugValue to be called inside another Hook like useEffect. So, to access the HTTP response status, another state variable must be used.

Step 3: Observing the results with the Hook

When the Hook is called and observed in React Dev Tools after setting up useDebugValue, the figure shown below can be observed.

Observing the results with the useDebugValue Hook

There, we see a new DebugValue array. This array outputs all the data we wish to debug in a more readable manner.

Note: The indexes of the DebugValue array are printed in sequential order of the calls made to the useDebugValue Hook.

Optimizing the useDebugValue Hook

Now, the question is, do we need formatting all the time? Isn’t it costly to format data every time a Hook is called or a state within a Hook is changed? The answer to the latter is yes.

However, React has a solution for that, and it is easier than you think! The Hook accepts a second parameter. This parameter is a formatting function that accepts the log data and renders the data only when the Hook is inspected with React Dev Tools, ensuring that the costly formatting operation is not executed every time the Hook is executed.

The following code includes the formatting function in useDebugValue.

useDebugValue(response, (res) => {
  // formatting function
  if (res) {
    // format and return the data when hook is inspected.
    return \`Data returned from API that shows local cases for today: ${res.data.local\_new\_cases}\`;
  }
  return 'No Data Returned At The Moment';
});

That’s it! We have successfully applied useDebugValue in our custom Hook to improve the overall debugging experience.

However, it is always essential to identify when to use this Hook.

When should you use useDebugValue?

Although useDebugValue is really useful, it is not good to include it in all custom Hooks.

The perfect use case of useDebugValue is when you have extremely complex code executing in your custom Hook with multiple state variables. Incorporating the useDebugValue Hook in a case like this will help filter out unnecessary logging and prioritize your code.

GitHub reference

Also, refer to the React custom Hooks debugging with the useDebugValue demo on GitHub.

Conclusion

This article explored the use of the useDebugValue Hook in debugging custom React Hooks. Having the useDebugValue in your development arsenal will increase your debugging capabilities for your custom React Hooks.

I hope you have found this useful. Thank you for reading.

The Syncfusion React suite offers over 70 high-performance, lightweight, modular, and responsive UI components in a single package. It’s the only suite you’ll ever need to construct a complete application.

If you have questions, you can contact us through our support forumsupport portal, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Share on twitter
Share on facebook
Share on linkedin
Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top