【发布时间】:2016-11-05 14:25:35
【问题描述】:
我有一个功能,您可以单击图像并查看可单击的名称列表....当您单击名称时,该人的图像应代替原始图像。我正在使用艺术家 api,而不是在控制台中出现错误,图像更改为名称为“未定义”的艺术家的 img ......奇怪。可能不是一个巨大的修复,但我已经被这个问题折磨了一段时间了。
searchForArtist(query) {
request.get(`https://api.spotify.com/v1/search?q=${query}&type=artist`)
.then((response) => {
const artist = response.body.artists.items[0];
const name = artist.name;
const id = artist.id;
const img_url = artist.images[0].url;
this.setState({
selectedArtist: {
name,
id,
img_url,
},
});
})
.then(() => {
this.getArtistAlbums();
})
.catch((err) => {
console.error(err);
});
}
getSubsequentCollabs(artist) {
this.setState({
selectedArtist: {},
selectedAlbums: {},
artistCounts: {},
});
console.log(artist);
this.searchForArtist(artist);
}
artistOnClick(e) {
console.log(e);
let artist = e.target.value;
this.getSubsequentCollabs(artist);
}
我有另一个组件这样做:
const Artist = ({name, artistOnClick}) => {
return (
<div name={name} onClick={artistOnClick}>
{name}
</div>
)
}
export default Artist;
【问题讨论】:
-
您正在收听
div组件的“onclick”事件。你对e.target.value有什么期待?! -
对不起,您能否详细说明或提供更多帮助。我可能没有你那么多经验。
-
也许是另一种解决方案...
e.nativeEvent.target.name