-1

Here is my path to the image: ../src/assets/images/img_name.jpg"
And path to the file.js: src/file_name.js
If I implement my code in file.js like this:

  1. Import img_name from "../src/assets/images/img_name.jpg"
  2. Then reference the img path in href tag: <img src={img_name}></img>

It will work but If I dont import and reference the img like this: <img src='../src/assets/images/img_name.jpg'></img>
It won't work no more. Can anyone explain why??
I use create-react-app

Viet
  • 1
  • 1
  • First of all, `img`s have no closing tag. Secondly, if the top path works why would you expect the second path to work? `/src/assets/images/img_name.jpg` probably doesn't also exist in the parent directory. – ggorlen Sep 17 '22 at 03:30
  • @ggorlen About img I think you can both use or . Or at least it works for me. And both of the paths i used are the same: `../src/assets/images/img_name.jpg`. The problem is if I import it works, but if I use the path in src attribute it doesn't work. – Viet Sep 17 '22 at 03:52
  • Just because it works for you doesn't mean it's legal HTML. Browsers are able to fix broken HTML. Your last edit changed the paths. – ggorlen Sep 17 '22 at 04:19
  • @ggorlen Thank you, I understand what you're saying. The path I edited is what I was the path that I wanna ask. And I only realise it after you said that lol... Anyway thank you. – Viet Sep 18 '22 at 04:32

1 Answers1

0

you have to explictly import image file since webpack need proper loader for this file