Skip to content

Latest commit

 

History

History
30 lines (22 loc) · 1.11 KB

void-dom-elements-no-children.md

File metadata and controls

30 lines (22 loc) · 1.11 KB

Prevent void DOM elements (e.g. <img />, <br />) from receiving children (react/void-dom-elements-no-children)

There are some HTML elements that are only self-closing (e.g. img, br, hr). These are collectively known as void DOM elements. If you try to give these children, React will give you a warning like:

Invariant Violation: img is a void element tag and must neither have children nor use dangerouslySetInnerHTML.

Rule Details

The following patterns are considered warnings:

<br>Children</br>
<br children='Children' />
<br dangerouslySetInnerHTML={{ __html: 'HTML' }} />
React.createElement('br', undefined, 'Children')
React.createElement('br', { children: 'Children' })
React.createElement('br', { dangerouslySetInnerHTML: { __html: 'HTML' } })

The following patterns are not considered warnings:

<div>Children</div>
<div children='Children' />
<div dangerouslySetInnerHTML={{ __html: 'HTML' }} />
React.createElement('div', undefined, 'Children')
React.createElement('div', { children: 'Children' })
React.createElement('div', { dangerouslySetInnerHTML: { __html: 'HTML' } })