1

React app, Is there any difference (performance, lifecycle or any other aspects I can't think about) between storing images in public folder and display them by using:

<img src="/image.jpg" />

or storing images in src folder and use them with:

<img src={require('../../assets/image.jpg')} />
Yuval Levy
  • 2,397
  • 10
  • 44
  • 73
  • 1
    You have a few perks by resolving the image by using require. Look at this: https://reactnative.dev/docs/images – Angel González Jun 02 '20 at 14:25
  • is it valid also for reactjs, or only for react native ? – Yuval Levy Jun 02 '20 at 14:29
  • 1
    Only for react native, sorry! Didn't notice the reactjs tag. In reactjs, the only difference is how the image is resolved. If you use require the image will be resolved automatically when built, but it will be always an URL. – Angel González Jun 02 '20 at 14:35
  • 1
    These points should be applicable to both reactjs and react-native: 1. Only the images that are actually used will be packaged into your app. 2. Images live in the same folder as your JavaScript code. Components are self-contained. 3. No global namespace, i.e. you don't have to worry about name collisions. 4. Images can be distributed via npm packages. - From the [docs](https://reactnative.dev/docs/images) @AngelGonzález provided. – Ajeet Shah Jun 02 '20 at 14:47
  • 1
    [Do I store Image assets in public or src in reactJS?](https://stackoverflow.com/q/44643041/2873538) – Ajeet Shah Jun 02 '20 at 14:50

0 Answers0