PNG image not displaying, using React - javascript

Hi does anyone know why my png file is not displaying, even if i set it outside of the button element, it does not work.
I have used a JPG and it works but PNG is not, is there any obvious reason why PNG would not be showing up.
Here is an image of my code
Image of code here

First of all wrap the src in {}
Then if using Webpack; Instead of: <img src={"./logo.jpeg"} />
You may need to use require:
<img src={require('./logo.jpeg')} />
Another option would be to first import the image as such:
import logo from './logo.jpeg'; // with import
or ...
const logo = require('./logo.jpeg); // with require
then plug it in...
<img src={logo} />
I'd recommend this option especially if you're reusing the image source.

I don't know the full context, but assuming this React project is using webpack or was created with create-react-app, the image files in your src directory are not automatically being copied to the built application,
Try importing the image to the file where you use it:
import orbImage from "./org.png";
Then use it like so:
<img src={orgImage} />

I will try to help you,
on the other hand for your next message try to review the formatting inspire you from the other posts thx
for your png maybe probleme is your src
// The image.png file must be in the same folder as the JS file
// We import the image using its path
import image from './image.png';
and maybe try to add width="200"

Related

ReactJS: image import problem with using api variable

I am creating weather card with openweather API and i want to use images like:
<img src={`${wicon}.png`} />
So, i have image named '04d.png' and {wicon} variable comes with '04d' code then i put .png at the end of the code but also i need to import image with name.How can i solve this problem? i also tried import image with {wicon} variable but doesnt work.
Note: the above code works like '04d.png' when i run the API and inspect the webpage but doesnt load.
Is there any method to use images without import like HTML?
EDIT: The problem solved when i uploaded the localserver and put {wicon} variable at the end of the localserver link.
Webpage

Having issues with my img src using React

I'm trying to use an image of a search icon on my site, but it's not loading. I keep making sure my directory to the image is correct and it all looks good to me. I am currently in my Navbar>Index.js file and trying to get to my folder assets/icons/search.png
<img src={require('../../assets/icons/search.png')} alt="Search" />
my directory
image not loading on my site
In React you have to first import the image before you can use it.
import Image from '../../assets/icons/search.png'
Then you can use it like this
<img src={Image} alt="Search" />

How to render images in React when the image names contain white spaces?

I'm using React to reference a number of (local) images that have white space in their names. E.g.
<img src={`/images/${user.name}.jpg`} />
However, React is unable to load/render the image. The image are in the /public folder and I double checked the file names/paths. I also tried using encodeURIComponent() for the path, but that didn't work either. However, when I removed the white space from the image name, React was able to render the image properly.
please encode file name using encodeURIComponent
<img src={`/images/${encodeURIComponent(user.name)}.jpg`} />

img element is not working in Ionic React

A straight forward image element does not seem to work in Ionic React.
I added an image in assets/images folder and gave the relative path in src of image element.
<img src="../../assets/images/image-1.jpeg" alt=""/>
I created a working example using CodeSandbox. Can somebody help me if I was doing anything wrong here?
Figured out the issue here. I had to put the images in assets folder which will be in public folder.
I moved my local image to public/assets/images/image-1.jpeg and then access that path in img element.
<img src="assets/images/image-1.jpeg" alt=""/>

Add animated gifs to react web apps

I'm trying to define a transition state for my react web application when I'm publishing data to backend.
I want to show an animated gif in my render method.
So I import the gif image (like this https://media.giphy.com/media/3oEjI6SIIHBdRxXI40/giphy.gif )
using a simple import
import logo from '../assets/load.gif'
and add it to my render
like:
<img src={logo} alt="loading..." />
but I get an error in my react-dev server terminal
unknown character
How to add animated gif's to a plain react SPA.
I had a version that used a URL however, it stopped working. I found a site that you can download the spinner as a GIF, then I put that .gif into my images folder. From there you can use:
import logo from '../assets/load.gif'
&
<img src={logo} alt="loading..." />
You can also use require('path') method
The require() method fetches the images and converts it to data URI format Read about Data URI here
<img src={require('../assets/load.gif')} alt="loading..." />
import logo from './gifPath.gif'
<img src={logo} alt="loading..." />
I used this and worked for me as I downloaded the gif
You just have to modify the path
Try something like:
import load from './assets/load.gif'

Categories