0

So I am having trouble loading images in my React code. I set up dummy data to load images locally.

My path is public/images/nyc.jpg

In my React file I have tried to wrap it with quotes like I normally do in mustaches but it doesn't work with react.

<img src='/public/images{this.props.image}' /> 

but it wouldn't work. Is there a way I can load the data dynamically or tell webpack for a specific jsx attributes that load from public/images path?

import React, { Component } from 'react' import styles from './styles' class Book extends Component { render(){ const style = styles.book return ( <div style={styles.container}> <h2 style={styles.header}> <div> <img src={this.props.image} /> </div> <div> <span>{this.props.book.title}</span><br /> </div> <a style={styles.title} href='#'>{this.props.book.author}</a> </h2> <span>{this.props.book.genre}</span><br /> <span>{this.props.book.publishDate}</span><br /> </div> ) } } export default Book 

my webpack code

var webpack = require('webpack') var path = require('path') module.exports = { entry: { app: './src/app.js' }, output: { filename: 'public/build/bundle.js', sourceMapFilename: 'public/build/bundle.map' }, devtool: '#source-map', module: { loaders: [ { test: /\.jsx?$/, exclude: /(nodle_modules)/, loader: 'babel', query: { presets: ['react', 'es2015'] } } ] } } 
4
  • 2
    Is this what you're looking for? src={'/public/images' + this.props.image}? Commented Nov 12, 2016 at 23:45
  • thanks guys. I really appreciate it. Commented Nov 13, 2016 at 0:26
  • 1
    if you're using webpack you need to require() images and have an appropriate loader, usually url-loader Commented Nov 13, 2016 at 1:32
  • 1
    I love this syntax: src={`/public/images/${this.props.image}`} note: those are back-ticks Commented Nov 13, 2016 at 3:50

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.