Debugging
Preact provides a powerful debugging addon that helps catch common mistakes and provides detailed error messages during development. Thepreact/debug package enhances your development experience with helpful warnings, prop type checking, and detailed component stack traces.
Enabling Debug Mode
To enable debug mode, importpreact/debug at the entry point of your application:
Error Detection
The debug addon catches many common mistakes and provides detailed error messages:Undefined Components
When you passundefined to createElement, debug mode provides a helpful error:
debug/src/debug.js:153-159
Invalid Component Types
Debug mode detects when you accidentally pass JSX literals or invalid types:debug/src/debug.js:160-176
Invalid Refs
Refs must be functions or objects created bycreateRef():
debug/src/debug.js:178-190
Event Handler Validation
Event handlers must be functions:debug/src/debug.js:192-208
Render Loop Detection
Debug mode prevents infinite render loops by tracking consecutive renders:debug/src/debug.js:262-269
HTML Nesting Validation
Debug mode validates proper HTML nesting according to web standards:Table Element Nesting
debug/src/debug.js:358-440
Paragraph Element Validation
debug/src/debug.js:416-428
Interactive Content Nesting
debug/src/debug.js:429-439
PropTypes Checking
Debug mode automatically checks PropTypes when defined on components: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.
debug/src/component-stack.js:78-100
Hook Validation
Debug mode validates hook usage:Hook Call Location
debug/src/debug.js:274-280
NaN in Dependencies
debug/src/debug.js:470-490
Lifecycle Warnings
Debug mode warns about incorrect lifecycle usage:setState in Constructor
debug/src/debug.js:494-511
forceUpdate on Unmounted Components
debug/src/debug.js:528-546
Suspense Validation
Debug mode ensures Suspense boundaries are properly configured:debug/src/debug.js:78-99
Duplicate Keys Warning
Debug mode detects duplicate keys in lists:debug/src/debug.js:446-468
Hydration Mismatch Detection
When using SSR with hydration, debug mode detects mismatches: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):debug/src/index.js:6, debug/src/check-props.js:8-10
Component Stack Utilities
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