Open In App

Give an example of using useDebugValue to inspect custom hooks?

Last Updated : 22 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

useDebugValue is a React hook that allows you to display a label for custom hooks in React DevTools. This can be useful for debugging and inspecting custom hooks.

Key Features:

  • Debugging Made Easier: useDebugValue is like a magnifying glass for custom hooks in React. It helps users see inside these hooks more clearly so they can understand what’s going on and find any issues more easily.
  • Custom Labels: useDebugValue as a sticky note, you can attach it to your custom hooks. You can write whatever you want on these sticky notes – maybe a reminder of what the hook does or some important information about it.
  • Works with React DevTools: Just like how sticky notes help you remember things, useDebugValue helps React DevTools shows you useful information about your custom hooks. When you inspect a custom hook in React DevTools, you’ll see the details you’ve written using useDebugValue.
  • Shows Information When Needed: You can decide when to show the information on your sticky note. Maybe you only want it to appear when something specific happens in your code. useDebugValue lets you do that, so you only see the details when they’re helpful.

Example: Below is an example of using useDebugValue to inspect custom hooks.

  • We create a custom hook useFetch that fetches data from a given URL using the fetch API.
  • Inside the custom hook, we use useDebugValue(url) to display the URL in React DevTools.
  • We use the custom hook useFetch inside the MyComponent component to fetch data from a specific URL.
  • When you inspect the useFetch hook in React DevTools, you’ll see the URL displayed as its label, making it easier to identify the purpose of the hook.

Javascript




import React, {
    useState,
    useEffect,
    useDebugValue
} from 'react';
 
// Custom hook to fetch data from an API
function useFetch(url) {
    const [data, setData] = useState(null);
    const [loading, setLoading] = useState(true);
 
    useEffect(() => {
        async function fetchData() {
            try {
                const response = await fetch(url);
                const result = await response.json();
                setData(result);
                setLoading(false);
            } catch (error) {
                console.error('Error fetching data:', error);
                setLoading(false);
            }
        }
 
        fetchData();
    }, [url]);
 
    /*
        Use useDebugValue to display
        the URL in React DevTools
    */
    useDebugValue(url);
 
    return { data, loading };
}
 
// Component that uses the custom hook
function MyComponent() {
    const { data, loading } =
        useFetch('https://fakestoreapi.com/products');
 
    if (loading) {
        return <div>Loading...</div>;
    }
 
    return (
        <div>
            {
                data ?
                    (<div>Data: {JSON.stringify(data)}</div>) :
                    (<div>No data available</div>)
            }
        </div>
    );
}
 
export default MyComponent;


Output:

gfg17

Output



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads