How to Debug React Components

Debugging React components is an essential skill for building robust, error-free web applications. This article provides a practical guide on how to identify and resolve issues in your React code. You will learn about key tools like React Developer Tools, browser developer consoles, VS Code debugging configurations, and effective coding strategies like using the useDebugValue hook and error boundaries to streamline your troubleshooting process.

Use React Developer Tools

The React Developer Tools extension (available for Chrome, Firefox, and Edge) is the most powerful tool for inspecting React applications. Once installed, it adds two tabs to your browser’s developer tools:

Leverage Console Logging and the debugger Keyword

While simple, strategic logging is often the fastest way to trace state changes.

Implement Error Boundaries

By default, if a JavaScript error occurs during rendering, React unmounts the entire component tree, resulting in a blank screen. Error Boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of crashing the whole application.

You can implement an error boundary using class components:

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError(error) {
    return { hasError: true };
  }

  componentDidCatch(error, errorInfo) {
    // Log the error to an external service
    console.error("ErrorBoundary caught an error", error, errorInfo);
  }

  render() {
    if (this.state.hasError) {
      return <h1>Something went wrong.</h1>;
    }
    return this.props.children; 
  }
}

Wrap your main components or specific feature modules with this component to isolate UI failures.

Debug Inside Visual Studio Code

You can debug your React application directly within VS Code by utilizing its built-in debugger alongside source maps (which are enabled by default in build tools like Vite and Create React App).

  1. Create a .vscode/launch.json file in your project root.
  2. Add a configuration to launch your browser pointing to your local development server:
{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "chrome",
      "request": "launch",
      "name": "Launch Chrome against localhost",
      "url": "http://localhost:5173",
      "webRoot": "${workspaceFolder}"
    }
  ]
}
  1. Set breakpoints in your VS Code editor by clicking to the left of the line numbers, start the debugger, and interact with your app.

Trace Custom Hooks with useDebugValue

When creating custom React hooks, it can be difficult to distinguish them inside React Developer Tools. You can use the built-in useDebugValue hook to display a custom, readable label for your hook in the DevTools Components tab.

import { useState, useDebugValue } from 'react';

function useFriendStatus(friendID) {
  const [isOnline, setIsOnline] = useState(null);

  // Show a label in DevTools next to this hook: e.g., "FriendStatus: Online"
  useDebugValue(isOnline ? 'Online' : 'Offline');

  return isOnline;
}