【发布时间】:2018-12-29 04:19:44
【问题描述】:
对不起,如果这个问题的措辞很奇怪,不知道如何描述它。我正在尝试制作它,因此当您单击专辑名称时,它会显示该专辑的所有曲目;目前它可以很好地记录到控制台,但是当我尝试渲染它时,要么只显示一个曲目名称,要么什么都不显示(这是我目前所在的位置)。这是我的搜索组件,感谢所有帮助。
export default class SearchScreen extends Component {
constructor() {
super();
this.state = {
value: '',
searchClicked: false,
backClicked: false,
albumInfoClicked: false,
searchAlbums: {
response: []
},
albums: [],
tracks: ['']
};
this.handleChange = this.handleChange.bind(this);
this.searchAlbums = this.searchAlbums.bind(this);
}
handleChange(event) {
this.setState({
value: event.target.value
});
}
//Gets list of albums
searchAlbums() {
spotifyWebApi.searchAlbums(this.state.value)
.then((response) => {
this.setState({
albums: response.albums.items ,
searchClicked: true,
});
});
}
render() {
var albumTrackInfo = [];
var albumInfoClicked = false;
//this is the part that matters
if(searchClicked) {
return this.state.albums.map((t, counter) => {
return t.artists.map((artistsArray) => {
var albumID = t.id;
const albumInfo = () => {
albumInfoClicked = true;
console.log(albumID)
//Gets list of tracks for the album ID
spotifyWebApi.getAlbumTracks(albumID)
.then(response => {
albumTrackInfo = response.items
return albumTrackInfo.map((albumTracks) => {
this.setState({
tracks: albumTracks.name,
albumInfoClicked: true
})
//this isnt getting renderd
return (
<div>
<li>{this.state.tracks}</li>
</div>
)
})
})
}
return (
<div class="result-container row">
<div class="row">
<img src={t.images[0].url} alt={t.name} class="image" height="256px" width="256px" data-toggle="tooltip" data-placement="top" title="Hooray!" /> {counter}
<div class="middle">
<div key={t.name} class="text">
<a href="#" onClick={() => albumInfo()}> {t.id} <br></br> {t.name} </a><br></br>
By: {artistsArray.name} <br></br>
<i class="fas fa-play"></i>
</div>
</div>
</div>
</div>
)
});
});
}
【问题讨论】:
-
这似乎是很多代码,最好做一个小例子
-
我编辑掉了不需要的部分并添加了一些 cmets
-
刚刚注意到
if(searchClicked)不应该是if(this.state.searchClicked)
标签: javascript html css reactjs jsx