HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavascriptreactCritical

React won't load local images

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
localwonreactloadimages

Problem

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 (
            
                
                    
                        foo
                    
                    
                        
                            Vzdělání
                        
                        
                    
                
            
        );
    }
}

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(
    
        
            
            
            
            
        
        
    ,
    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

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 ` you need to use ` 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.

Context

Stack Overflow Q#34582405, score: 484

Revisions (0)

No revisions yet.