Html-react-parser: Error: img is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML`

Created on 26 Aug 2016  ·  14Comments  ·  Source: remarkablemark/html-react-parser

Hi!

Great plugin, really think this is going to be helpful for what I'm working on.

I noticed that parsing any img-element causes the above error. This may also apply to any element that never should have child elements.

A temporary workaround is to use something like this:

const parserConfig = {
    replace: domNode => {
        if(domNode.type === DOM_TYPE_TAG && domNode.name === 'img') {
            return <img src={domNode.attribs.src} alt={domNode.attribs.alt} className={domNode.attribs.class} />;
        }
    }
};
bug

Most helpful comment

I was running into this issue because I was trying to add children to an img tag...

All 14 comments

Thanks for letting me know. Do you have an example so I can reproduce this error?

Hi!
You can reproduce this with the following:
ReactDOM.render(Parser('<div><img src="" alt=""/></div>'), document.getElementById('root'));

Awesome, thank you!

@poacher2k Thanks for the example. I've looked into the bug and made a fix in #16.

Try npm update html-react-parser or npm install [email protected] and let me know if this removes the error for you.

That fixed it, thanks again! Glad to see you also added checks for other void elements!

Great to hear and you're very welcome! 👍

@remarkablemark I am still facing the issue and npm install [email protected] code is not updated in this version.

@vidit1 Can you confirm that you installed the latest version?

npm i html-react-parser@latest

# or
npm i [email protected]

Then verify by doing:

npm ls html-react-parser

@remarkablemark I updated the latest version html-react-parser and still face this issue. My element is inside a material-ui component.
<Drawer> <div className="alert" role="alert"> <img src="https://getbootstrap.com/assets/brand/bootstrap-solid.svg" width="30" height="30" className="d-inline-block align-top" alt=""> Bootstrap </img> </div> </Drawer>

@blueskysmart : You have the text "Bootstrap" inside your img-tag. As the error says, img is a void element, and cannot have any children.

See @remarkablemark 's comment below for guidance on how to fix it.

@blueskysmart The comment made by @poacher2k is correct. You can see the reproduction of this error in this fiddle.

Okie, I see. Thank you.

This error comes up also if you only try to parse this:
<img src="img.jpg".>

To make it work, wrap it in a div:

<div>
<img src="img.jpg".>
</div>

I was running into this issue because I was trying to add children to an img tag...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kartikag01 picture kartikag01  ·  5Comments

purp1eeeee picture purp1eeeee  ·  15Comments

on2air picture on2air  ·  3Comments

linkurzweg picture linkurzweg  ·  8Comments

rscott78 picture rscott78  ·  11Comments