Skip to main content

Debugging

Preact provides a powerful debugging addon that helps catch common mistakes and provides detailed error messages during development. The preact/debug package enhances your development experience with helpful warnings, prop type checking, and detailed component stack traces.
The debug addon should only be imported in development environments. Never include it in production builds as it adds overhead and exposes detailed error information.

Enabling Debug Mode

To enable debug mode, import preact/debug at the entry point of your application:
Import preact/debug as early as possible in your application to catch errors throughout your component tree.
With your bundler, you can conditionally import debug mode:

Error Detection

The debug addon catches many common mistakes and provides detailed error messages:

Undefined Components

When you pass undefined to createElement, debug mode provides a helpful error:
Source: debug/src/debug.js:153-159

Invalid Component Types

Debug mode detects when you accidentally pass JSX literals or invalid types:
Source: debug/src/debug.js:160-176

Invalid Refs

Refs must be functions or objects created by createRef():
Source: debug/src/debug.js:178-190

Event Handler Validation

Event handlers must be functions:
Source: debug/src/debug.js:192-208

Render Loop Detection

Debug mode prevents infinite render loops by tracking consecutive renders:
The limit is set to 25 consecutive renders. Source: debug/src/debug.js:262-269

HTML Nesting Validation

Debug mode validates proper HTML nesting according to web standards:

Table Element Nesting

Source: debug/src/debug.js:358-440

Paragraph Element Validation

Source: debug/src/debug.js:416-428

Interactive Content Nesting

Source: debug/src/debug.js:429-439

PropTypes Checking

Debug mode automatically checks PropTypes when defined on components:
Source: debug/src/debug.js:210-242, debug/src/check-props.js:24-54

Component Stack Traces

Debug mode provides detailed component stack traces showing where errors occurred:

Enabling Source Locations

For detailed file and line information, add the Babel plugin:
Without the jsx-source plugin, stack traces will show component names but not file locations.
Source: debug/src/component-stack.js:78-100

Hook Validation

Debug mode validates hook usage:

Hook Call Location

Source: debug/src/debug.js:274-280

NaN in Dependencies

Source: debug/src/debug.js:470-490

Lifecycle Warnings

Debug mode warns about incorrect lifecycle usage:

setState in Constructor

Source: debug/src/debug.js:494-511

forceUpdate on Unmounted Components

Source: debug/src/debug.js:528-546

Suspense Validation

Debug mode ensures Suspense boundaries are properly configured:
Source: debug/src/debug.js:78-99

Duplicate Keys Warning

Debug mode detects duplicate keys in lists:
Source: debug/src/debug.js:446-468

Hydration Mismatch Detection

When using SSR with hydration, debug mode detects mismatches:
Source: debug/src/debug.js:582-590

Exported Utilities

The debug package exports several utilities for advanced use cases:

resetPropWarnings()

Reset the history of PropTypes warnings (useful in tests):
Source: debug/src/index.js:6, debug/src/check-props.js:8-10

Component Stack Utilities

Source: debug/src/index.js:8-14

Disabling in Production

Always exclude debug from production builds:

Webpack

Vite

Performance Impact

The debug addon adds significant overhead:
  • Validates all vnodes before and after rendering
  • Checks HTML nesting rules
  • Validates PropTypes on every render
  • Maintains component stack traces
  • Adds ~10-20KB to bundle size
Never ship debug mode to production. It will significantly slow down your application and increase bundle size.