7 Ways to Use Images in ReactJS | React Images Tutorial | Learn ReactJS Assets Handling

preview_player
Показать описание
7 different ways to use images in React JS. There are multiple ways to use images in react js. Depending on your requirement, you can call each image individually or you can create an object, list images in a file and call these from there by using map function.

One way to use image in react is to import it with a name and then use that name in image source. You need ./ in path in my case. ./ means in same directory. Without it import won't work. You can call image from src folder or from anywhere in your app by using this method.

Our second way is to require image. If simple require statement does not work, use .default after require. You need ./ in path here in my case. Without it require won't work. You can call image from src folder or from anywhere in your app by using this method.

You can call individual image from Public directory easily. I have images folder in public directory. Public directory is not processed by webpack. You dont need import or require statement to call images from public directory. Just place images in public directory and call these like you do in plain HTML. In ReactJS app public directory is outside of src folder. If we use exact path including ../public/ it won't work but ./ will work. But you can skip ./ in this case.

You can use remote image from url in ReactJS. You can use generic img tag and src as you do in HTML to display a remote image in react js from an image url. We will use remote image url from a site:

You can also place image url in a variable and call it.

We used images one by one in these examples. What if we want to keep image urls in separate file and call images from there one by one depending on requirement or What if we want to to apply map function to call all images from a separate file?

You can use an object in a JS file to call multiple sets of images in one import. Lets create a JavaScript file in img directory that is in src directory. We will use require statement here. Just import the object and point to images in it.

You can use JSON file to list images, import it with any name and use map function to display images in react js. And we are calling images from public directory in this case. We are not exporting this JSON object in this case but we will import it.

We can call images from an object from a JS file while images are in public directory. We will use javascript file from src directory in this case and images from public directory. Then use map function to display images. Learn how to add image in react js or add image in react js. React image is used with simple img tag in JSX but there are few things that are important in case of using image in react js or react images. Also learn how to import image in react js or display image in react js. Easy way to import image in react js. Fix broken image react js.

More Ways to Use Images in React JS

- Multiple Images in One Import in React JS

- Easy Way to Use Images in React JS / No Import or Require

- Painless Images Import in React JS / One Import for Multiple Images in React JS

- Images from JSON file in React

- Display Icons / Images from JSON file in React JS

- Require Image Not Working in React JS

- ReactJS Playground

Chapters:
00:00 Intro
01:21 Import Image in ReactJS
03:09 Require Image in ReactJS
04:34 Use Image from Public Directory in ReactJS
06:50 Use Remote Image by Using URL in ReactJS
09:08 Use Images from Same Directory Using a JS File (multiple sets of images in one import)
13:37 Use Images from Public Directory by Uisng JSON File in ReactJS
17:35 Use Images from an Object and JS File (Public Directory)

👍 LIKE VIDEO
👊 SUBSCRIBE
🔔 PRESS BELL ICON
✍️ COMMENT

#reactjs #react #js #webdevelopment #javascript #javascripttutorial #webstylepress
Рекомендации по теме
Комментарии
Автор

To watch more related videos, please LIKE, SHARE & SUBSCRIBE! WebStylePress needs your support.

webstylepress
Автор

Thankyou so much!! I spent almost all day trying to load local images using require from the src folder and couldnt find anything useful on stack overflow, your solution was the simplest and easiest, cant state how much I appreaciate this video, Thanks again and again!!

teras
Автор

Thank you for this video tutorial!! I appreciate the fact that you use 'what if' scenerios in your tutorial. Many so-called js 'teachers' dont do that.

lancen
Автор

After trying with so much videos, only your (first way) worked for me. Bless u man <3

justgabrro
Автор

thank you i had error with require and method from here saved me

mr.darkness
Автор

You know this content is very resourceful, showing 7 different ways to include images in react - helped me overcome the hurdle of including images in ReactJs.

Thanks, much appreciated.

odedejikehinde
Автор

Thank you, I had been trying to figure out the JSON method for a few days now

tcutinthecut
Автор

many many thanks bro . i has this type problem (10min) i solve this problem. love from bangladesh

BohemianOnTheRoad
Автор

You helped me solve the exact problem I was facing. Thank you!

Dmooney
Автор

Thank you very much. Simple explanation.

nadunpabasara
Автор

thank you man it was very helpful for me

alanpuch
Автор

Great explanation. I always see different methods of using images in react js that confuse. This video shows every method in detail. Thank you for that. Which method is best or preferable?

tvboxhome
Автор

Amazing thank you! you solved my problem!

Aaron-syyx
Автор

Ignore my previous comment I have solved my issue. Thanks so much for the video! I have subscribed.

Ben-ntzl
Автор

Best video on react images thanks alot.

kartikeyjangir
Автор

but when make source image in setState then i put it in src doesn't working why ?

K-
Автор

Am using the last method in which my images are in public folder and js file src folder .But still am getting the error module not found

faisalrasheed