簡體   English   中英

React img 被危險地設置在 html 內部?

[英]React img is dangerously set inner html?

這是我擁有的一些代碼,並且給了我一個錯誤。 我不知道錯誤是什么意思以及為什么它不顯示。

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>
  )
}

我收到以下錯誤:

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

這是什么意思?

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這個

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>
    )
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM