babel-plugin-validate-jsx-nesting
Babel plugin to validate JSX nesting at compile time
The Problem
Certain HTML elements have nesting restrictions. For example, placing an <hr> inside a <p> is invalid. When browsers encounter this, they silently modify the DOM to fix it.
If you write this JSX:
The browser creates this DOM:
The paragraph gets split, and the <hr> ends up outside of it. This mismatch between your JSX and the actual DOM causes hydration errors in React and other frameworks that assume the DOM matches what was rendered.
The Solution
This Babel plugin catches invalid nesting at compile time. Instead of discovering the bug in production through a cryptic hydration error, you get a clear message during development:
Setup
Install babel-plugin-validate-jsx-nesting and add it to your Babel config:
To log warnings instead of failing the build, use the warnOnly option: