Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add image in react by using parcel?

I have been trying to add image in react. I'm not using webpack, I'm using parceljs. Also using typescript I have try:

import image from path/to/image.png <img src={image} />

inside react component:

try: <img src="path/to/image.png" />

try: <img src={"path/to/image.png"} />

Still, doesn't work.

code look sort of like this:

import * as React from 'react';

const componentName = () => (
  <div>
     <img src="path/to/image.png" />
  </div>
);
like image 680
Josue Toledo Avatar asked Oct 16 '18 18:10

Josue Toledo


People also ask

What is a parcel image?

Parcel includes an image transformer out of the box, which allows you to resize images, convert them to a different format, or adjust the quality to reduce file size. This can be done using query parameters when referencing the image, or using a configuration file.

How do I add an image to a div React?

Put the image file somewhere under the src folder. This alone will not automatically make it available, so you have to import the image into the React component where you're using it. import companyLogo from './path/to/logo.


Video Answer


2 Answers

You need to do it like this

import image from 'path/to/image.png';

And then inside your react JSX follow below code:

<img src={image} />
like image 61
Siya Mzam Avatar answered Oct 11 '22 22:10

Siya Mzam


It is no different between <img src="path/to/image.png"/> and <img src={"path/to/image.png"}/>, you should import your image and then use it like a JavaScript object, see below:

import somePhoto from 'path/to/image.png';

You don't attend to 'path/to/image.png'; and wrote it like nothing. input your path in a quotation mark. Then inside your react JSX code write your img tag like below:

<img src={somePhoto} />

There are more different ways. in other react projects we use another server to load the images. but the specific images for application should be like above.

like image 39
AmerllicA Avatar answered Oct 11 '22 21:10

AmerllicA