Rule proposal: prevent some DOM elements from having children or dangerouslySetInnerHTML
See original GitHub issueThere are some HTML elements that are normally self-closing (e.g. img
, br
, hr
). 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 usedangerouslySetInnerHTML
.
It would be nice to have a rule enforcing this.
Bad:
<img src="path/to/img.jpg">
Children
</img>
<img src="path/to/img.jpg" dangerouslySetInnerHTML={{ __html: 'HTML' }} />
<img src="path/to/img.jpg" children="Children" />
Good:
<img src="path/to/img.jpg" />
The full list that React uses can be found:
- https://github.com/facebook/react/blob/85dcbf83/src/renderers/dom/shared/ReactDOMComponent.js#L421
- https://github.com/facebook/react/blob/85dcbf83/src/renderers/dom/shared/ReactDOMComponent.js#L449
Which comes out as:
area
base
br
col
embed
hr
img
input
keygen
link
menuitem
meta
param
source
track
wbr
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
prevent some DOM elements from having children or ... - GitHub
Invariant Violation: img is a void element tag and must neither have children nor use dangerouslySetInnerHTML . It would be nice to have...
Read more >React.js: Set innerHTML vs dangerouslySetInnerHTML
The immediate effect of using innerHTML versus dangerouslySetInnerHTML is identical -- the DOM node will update with the injected HTML.
Read more >DOM Elements - React
React implements a browser-independent DOM system for performance and cross-browser compatibility. We took the opportunity to clean up a few rough edges in ......
Read more >Using dangerouslySetInnerHTML - Getting Started with DOM
Yes, dangerouslySetInnerHTML is a property and requires only a HTML element to render. 2. Is it OK to use dangerouslySetInnerHTML? Yes, it will ......
Read more >Using dangerouslySetInnerHTML in a React application
A use case where you need to set the HTML content of a DOM element is when you populate a <div> element with...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
That works for me!
In that case, maybe
void-dom-elements-no-children
? “tag” isn’t appropriate if it’s not just jsx, and i like including “dom” to point out that it’s not for custom components.