Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

structure of img directory react.js

How on earth do i get images to display? 40 mins reaserching and still no joy. I think the real answer is where do i put images.

structure of the main dir

App.js
public > index.js 
Views >
      index.jsx
      layouts >
          footer.jsx
          header.jsx
          master.jsx

Wherever i put the images they won't display. the main file that gets called and renders the html is index.jsx inside the views folder.

I am using express to create the virtual server and as the core engine.

i have tried the following inside the header.jsx file

<img src='images/main_home_icon.png'/>
<img src={require('images/main_home_icon.png')} />
like image 683
Paddy Avatar asked Apr 19 '16 08:04

Paddy


People also ask

Is there a recommended way to structure react projects?

Is there a recommended way to structure React projects? React doesn’t have opinions on how you put files into folders. That said there are a few common approaches popular in the ecosystem you may want to consider. One common way to structure projects is to locate CSS, JS, and tests together inside folders grouped by feature or route.

Is it possible to keep your SRC folder clean in ReactJS?

But as your project becomes larger, it will become pretty impossible for you to maintain your files and keep your src folder clean. Also if you are going to work in a team a folder structure would be followed and we are going to talk about the most widely used folder structure here. Here is an obligatory introduction to ReactJs.

Is it possible to add image to public directory without react-app?

Just remember that you need to restart the app if you added new image to the public directory. It is indeed a fast way to treat the situations, but it will work only if you don't need relative URLs. E.g. if you serve your app at www.example.com/react-app, the public folder will be exposed on www.example.com, without react-app.

Is there a react solution for path of an image?

He also mentions you can use babel-plugin-transform-react-jsx-img-import to get around having to import the image every time you reference an image. Not this, it solves only image issue but does not solve the path issue.. Dima Gershman' answer is the actual react solution for path of any file img or else


1 Answers

It depends. If you use the <img src='images/main_home_icon.png'/> form, then your base path is the public directory. Normally you create a bundle.js with browserify or webpack and put the javascript file in a directory with an index file. You would then have to put the images in the same directory.

On the other hand you can include your images directly in your javascript code with:

<img src={require('images/main_home_icon.png')} />

Then your base path is the base path of the jsx file (in your case: layouts folder). I would advise to use this method because you can define e.g. in your webpack config which images (depends on their size) should be included in your bundle file as a base64 string and which are included only with their relative path.

For further reading I would suggest to take a look at this "cookbook": https://github.com/christianalfoni/react-webpack-cookbook

like image 131
CapCa Avatar answered Nov 14 '22 21:11

CapCa