简体   繁体   中英

React img is dangerously set inner html?

Here is some code I have and is giving me an error. I have no idea what the error means and why it's not displaying.

import React from "react";
import "./Card.css";

export default function Card() {
  return (
    <div class='card'>
        <img src='img_avatar.png' alt="Avatar" style='width:100%'> </img>
        <div class='container'>
            <h4><b>Devware</b></h4>
            <p>Description</p>
        </div>
    </div>
  )
}

I'm getting the following error:

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

What does this mean?

This is because you have a space between open <img> and closing </img> tags which is invalid, also React style prop should be an object and class should be className as class is a reserved javascript word so your Card component should be like this

export default function Card() {
    return (
        <div className='card'>
            <img src='img_avatar.png' alt="Avatar" style={{ width: '100%' }} />
            <div className='container'>
                <h4><b>Devware</b></h4>
                <p>Description</p>
            </div>
        </div>
    )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM