44

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

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?

5 Answers 5

60

As you do in 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);
Sign up to request clarification or add additional context in comments.

2 Comments

Using this code I received the following error: Error: img is a void element tag and must neither have children nor use dangerouslySetInnerHTML.
@SamuelRosenstein try codeSanbox to reproduce the issue.
15

To solve this;

For local images:

Use an import statement No need to pollute the public folder

import slideImg1 from '../../assets/pexels-pixabay-259915.jpg';

Then use in Jsx like this

    const solveLocalImg = () => (
<img src={slideImg1}/>

//or

<div data-src={slideImg1}>
      </div>
)

For online images

Use an array

let imgs = [
  'https://res.cloudinary.com/stealthman22/image/upload/v1586308024/new-portfolio/hero/time-lapse-photography-of-waterfalls-during-sunset-210186.jpg',
  'https://res.cloudinary.com/stealthman22/image/upload/v1586308023/new-portfolio/hero/two-cargo-ships-sailing-near-city-2144905.jpg',
];

 const solveOnlineImg = () => (
<div>
<img src={imgs[0]}/>
<img src={imgs[1]}/>
</div>
)

You can use as many images as you like with the second method. It even makes it easy for you to manage your images. Hopefully, soon enough we'd either get a solution that can make us do things how we are used to with just HTML CSS and js

For now, we are stuck with amazing Webpack

3 Comments

which path is the correct if I place a folder inside /resources/js/Pages/Landing/imglanding/B.png
I don't get your question. But I believe you should always use the full path unless your code file is right in the same folder as the image, which I doubt. I think vs code even does a good job of showing you the path once you press "./" It should be the same for all other code editors.
thats true, I mean, it was my bad, always remember that if you are importing an image your path should be from the file you are importing not from root.
5

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

Comments

3

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

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

Comments

0

In the App.js file, write following code:

import React from 'react';

function App() {

  return(
    <div>
      <img src="image-url" alt="image" />
    </div>
  );
}

export default App;

2 Comments

What's different between this answer and the accepted answer?
The accepted answer shows error (Error: img is a void element tag and must neither have children) which can be resolved by returning every thing inside a div container. It is also a good practice to return every thing as one entity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.