Javascript 在 ReactJS 中显示来自 url 的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51184136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:47:08  来源:igfitidea点击:

Display an image from url in ReactJS

javascriptreactjsimage

提问by Last

I want to display an image from a URL in React. For example, I want this image

我想在 React 中显示来自 URL 的图像。例如,我想要这张图片

https://images.pexels.com/photos/20787/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=350

to be displayed in a Card body or reactJS. Is it possible to do it or do I have to download it to assets before I can display it? And how to do it?

显示在 Card body 或 reactJS 中。是否可以这样做,还是必须将其下载到资产才能显示?以及怎么做?

回答by Suman Kundu

As you do in HTML

正如你在 HTML 中所做的那样

 import React from "react";
 import ReactDOM from "react-dom";

 function App() {
   return (
     <img 
      src="https://images.pexels.com/photos/20787/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=350"
      alt="new"
      />
   );
 }

 const rootElement = document.getElementById("root");
 ReactDOM.render(<App />, rootElement);

回答by Harikrishnan

You can use tag for displaying the image.If the image source is in props/state use <img src={this.props.url}/>

您可以使用标签来显示图像。如果图像源在道具/状态使用 <img src={this.props.url}/>

回答by Dinesh undefined

Yes. it is possible. just use <img />tag.

是的。有可能的。只需使用<img />标签。

<img src="https://images.pexels.com/photos/20787/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=350">