question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: prevent some DOM elements from having children or dangerouslySetInnerHTML

See original GitHub issue

There 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 use dangerouslySetInnerHTML.

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:

Which comes out as:

  • area
  • base
  • br
  • col
  • embed
  • hr
  • img
  • input
  • keygen
  • link
  • menuitem
  • meta
  • param
  • source
  • track
  • wbr

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lencionicommented, Jul 26, 2016

That works for me!

1reaction
ljharbcommented, Jul 26, 2016

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found