React won't load local images
I am building a small react app and my local images won't load. Images like placehold.it/200x200
loads. I thought maybe it could be something with the server?
Here is my App.js
import React, { Component } from 'react';
class App extends Component {
render() {
return (
<div className="home-container">
<div className="home-content">
<div className="home-text">
<h1>foo</h1>
</div>
<div className="home-arrow">
<p className="arrow-text">
Vzdělání
</p>
<img src={"/images/resto.png"} />
</div>
</div>
</div>
);
}
}
export default App;
index.js:
import React, { Component } from 'react';
import { render } from 'react-dom';
import { Router, Route, Link } from 'react-router';
import { createHistory } from 'history';
import App from './components/app';
let history = createHistory();
render(
<Router history={history} >
<Route path="/" component={App} >
<Route path="vzdelani" component="" />
<Route path="znalosti" component="" />
<Route path="prace" component="" />
<Route path="kontakt" component="" />
</Route>
<Route path="*" component="" />
</Router>,
document.getElementById('app')
);
and server.js:
var path = require('path');
var express = require('express');
var webpack = require('webpack');
var config = require('./webpack.config.dev');
var app = express();
var compiler = webpack(config);
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}));
app.use(require('webpack-hot-middleware')(compiler));
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.listen(3000, 'localhost', function(err) {
if (err) {
console.log(err);
return;
}
console.log('Listening at http://localhost:3000');
});
Solution 1:
When using Webpack you need to require
images in order for Webpack to process them, which would explain why external images load while internal do not, so instead of <img src={"/images/resto.png"} />
you need to use <img src={require('/images/image-name.png')} />
replacing image-name.png with the correct image name for each of them. That way Webpack is able to process and replace the source img.
Solution 2:
I started building my app with create-react-app (see "Create a New App" tab). The README.md that comes with it gives this example:
import React from 'react';
import logo from './logo.png'; // Tell Webpack this JS file uses this image
console.log(logo); // /logo.84287d09.png
function Header() {
// Import result is the URL of your image
return <img src={logo} alt="Logo" />;
}
export default Header;
This worked perfectly for me. Here's a link to the master doc for that README, which explains (excerpt):
...You can import a file right in a JavaScript module. This tells Webpack to include that file in the bundle. Unlike CSS imports, importing a file gives you a string value. This value is the final path you can reference in your code, e.g. as the src attribute of an image or the href of a link to a PDF.
To reduce the number of requests to the server, importing images that are less than 10,000 bytes returns a data URI instead of a path. This applies to the following file extensions: bmp, gif, jpg, jpeg, and png...
Solution 3:
Another way to do:
First, install these modules: url-loader
, file-loader
Using npm: npm install --save-dev url-loader file-loader
Next, add this to your Webpack config:
module: {
loaders: [
{ test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192' }
]
}
limit
: Byte limit to inline files as Data URL
You need to install both modules: url-loader
and file-loader
Finally, you can do:
<img src={require('./my-path/images/my-image.png')}/>
You can investigate these loaders further here:
url-loader: https://www.npmjs.com/package/url-loader
file-loader: https://www.npmjs.com/package/file-loader
Solution 4:
Best way to load local images in react is as follows
For example, Keep all your images(or any assets like videos, fonts) in the public folder as shown below.
Simply write <img src='/assets/images/Call.svg' />
to access the Call.svg image from any of your react component
Note: Keeping your assets in public folder ensures that, you can access it from anywhere from the project, by just giving '/path_to_image' and no need for any path traversal '../../' like this
Solution 5:
By doing a simple import you can access the image in React
import logo from "../images/logo.png";
<img src={logo}/>
Everything solved! Just a simple fix =)