1
我試圖顯示與圖像作出反應如何在Reactjs中動態渲染圖像?
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'
}
}
這裏渲染
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>}
);
}
我該怎麼辦?
除非你有自定義的模塊加載器,'require'不會「加載」圖像文件。 'require'默認只能理解JSON和JS。 –