1

I am trying to display an image with React

const falsyData = {
    'Agent Swartz': {
      'username' : 'Kakaroto',
      'position': 'Agent',
      'picture': require('./img/aaron.jpg'),
      'area': 'Canada'
    },
    'Agent Larry': {
      'username' : 'Larry',
      'position': 'User',
      'picture': require('./img/larry.jpg'),
      'area': 'France'
    }
  }

here is the render

constructor() {
    super();
    this.state = {value : '', result: ''};
  } 
render () {
    let searchRes = this._matchPeople(this.state.value),
        match = Object.keys(searchRes).map(function(key) {
          return <div><h3>{key}</h3>
                Username: <strong>{searchRes[key].username}</strong> <hr />
                Position: <strong>{searchRes[key].position}</strong> <hr />
                Picture: <strong>{searchRes[key].username}</strong>  <hr />
                Area: <strong>{searchRes[key].area}</strong>         <hr />
                <img src={searchRes[key].picture} />  // HERE I NEED THE IMG
          </div>;
        });
    return (
            <TextField onChange={this._onChange.bind(this)}
                    onKeyUp={this._changeInput.bind(this)}
                    value={this.state.value} />
            {!!this.state.value.length && <div>{match}</div>}
    );
  }

what should I do ?

1
  • Unless you have a custom module loader, require will not "load" image files. require only understands JSON and JS by default. Commented Sep 4, 2015 at 2:45

1 Answer 1

1

Since the src attribute of <img> takes a url, I would change your falsyData to

const falsyData = {
  'Agent Swartz': {
    'username' : 'Kakaroto',
    'position': 'Agent',
    'picture': '/img/aaron.jpg',
    'area': 'Canada'
  },
  'Agent Larry': {
    'username' : 'Larry',
    'position': 'User',
    'picture': './img/larry.jpg',
    'area': 'France'
  }
}

This way you're passing a url to the <img> element instead of whatever require('path/file.jpg') returns.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.