react js 添加樣式
Hello! In this article, we will learn how to add images in React JS? I remember when I just started coding in React JS, I thought adding images would be done exactly as it is in HTML. I later realized that it was different.
你好! 在本文中,我們將學習如何在React JS中添加圖像? 我記得當我剛開始在React JS中編碼時,我以為添加圖像將完全像在HTML中那樣進行。 后來我意識到這是不同的。
Let's quickly look at its syntax or how it is done?
讓我們快速看一下它的語法或如何完成的?
Just like in HTML, having the image in the same folder with the image makes it easy getting the file location URL.
就像在HTML中一樣,將圖像與圖像放在同一文件夾中可輕松獲取文件位置URL。
We equally ought to know the extension with our image.
我們同樣應該知道我們形象的延伸。
Open your index.js file and type the following as usual,
打開index.js文件,然后像往常一樣鍵入以下內容,
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
ReactDOM.render(<App />, document.getElementById('root'))
From React JS basics, the code above simply imports the required dependencies, components and renders everything to the root node.
從React JS基礎開始,以上代碼僅導入所需的依賴項,組件并將所有內容呈現到根節點。
import React from "react"
import Img from './congrats.png'
class App extends React.Component {
render() {
return (
<div>
<center>
<img src= {Img} alt="pic" />
<br/> <b> CONGRATS </b>
</center>
</div>
)
}
}
export default App
From the code above, we added the image congrats.png using the syntax <img src = {Img} alt="pic" />.
從上面的代碼中,我們使用語法<img src = {Img} alt =“ pic” />添加了圖像congrats.png 。
We first of all import the image as seen in the second line above.
首先,如上第二行所示,導入圖像。
The term Img is not conventional and any name can be used, provided it is equally used the same when writing the image tag.
術語Img不是常規名稱,可以使用任何名稱,只要在寫入圖像標簽時使用相同的名稱即可。
Congrats.png is the name of my image and its an extension is PNG.
Congrats.png是我的圖像的名稱,其擴展名為PNG。
When writing the tag, we enclose {Img} in curly braces and the tag ends with / > which is a convention in JSX.
編寫標記時,我們將{Img}括在花括號中,并且標記以/>結尾,這是JSX中的約定。
Also note that, all react JS image tags must have an alternate (alt).
另請注意, 所有react JS圖像標簽都必須具有備用 ( alt )。
Output
輸出量

Thanks for coding with me! See you @ the next article. Feel free to drop a comment or question.
感謝您與我編碼! 下次見。 隨意發表評論或問題。
翻譯自: https://www.includehelp.com/react-js/how-to-add-an-image-in-react-js-application.aspx
react js 添加樣式