custom

useDebugValue

Labels a custom hook's value in React DevTools so it is easier to identify while debugging. Reach for it inside reusable custom hooks — it has no effect for end users of your component.

useDebugValue(value, format?)

Parameters & Behavior

Parameter Purpose
value The value or description shown next to the hook name in DevTools
format Optional formatter function — only called when DevTools inspects the hook (deferred)
no runtime cost In production the formatter isn't invoked unless a devtools panel is open
custom hooks only Placing it in a component is legal but adds no useful info

Examples

function useOnlineStatus() {
  const online = useSyncExternalStore(subscribe, () => navigator.onLine);
  useDebugValue(online ? 'Online' : 'Offline');
  return online;
}

DevTools shows 'OnlineStatus: Online' next to the custom hook

function useUser(id) {
  const user = useContext(UserContext);
  useDebugValue(user, u => u ? `${u.name} (${u.id})` : 'anon');
  return user;
}

Deferred formatter runs only when the hook is expanded in DevTools

function useDebouncedValue(value, ms) {
  const [v, setV] = useState(value);
  useEffect(() => { const id = setTimeout(() => setV(value), ms); return () => clearTimeout(id); }, [value, ms]);
  useDebugValue(`${v} (${ms}ms)`);
  return v;
}

Custom debounce hook labeling its current value and delay

useDebugValue(items.length, n => `${n} items`);

Formatter avoids building the string unless DevTools asks for it

Gotcha

The formatter must be pure — DevTools may call it any time, or not at all. Do not rely on side effects here.

Related hooks

← All React hooks · Fix hydration mismatch