【发布时间】:2020-09-17 16:27:01
【问题描述】:
如何在 react jsx 的数组“tracks”中一次迭代一个索引。我希望能够在数组中一次只迭代和显示一个轨道,并通过单击标记为“下一个”的按钮转到下一个轨道(索引),当点击“上一个”按钮。
constructor(props){
super(props);
this.state=({
year: (props.location.state && props.location.state.year) || '',
all_tracks: {tracks:[]},
currentTrack: 0,
});
}
onClickNext(){ //Next button
//Nothing done here yet
}
onClickPrev(){ //Previous button
//Nothing done here yet
}
render(){
const {
currentTrack,
all_tracks: { tracks }
} = this.state;
return (
window.addEventListener('DOMContentLoaded', (event) => {
this.gettingTracks() //Have tracks load immediately
}),
<div id = "song"> //THIS PART
<iframe id="track" src={tracks[currentTrack]
? "https://open.spotify.com/embed/track/"+tracks[currentTrack].id
: "Song N/A"} ></iframe>
</div>
<div>
<button id="nextBtn"> Next </button>
</div>
<div>
<button id="prevBtn"> Previous </button>
</div>
);
}
这是我填充轨道数组的地方
gettingTracks(){
// store the current promise in case we need to abort it
if (prev !== null) {
prev.abort();
}
// store the current promise in case we need to abort it
prev = spotifyApi.searchTracks('genre: pop year:' + this.state.year, {limit: 20, offset:1});
prev.then((data) => {
this.setState({
all_tracks: {
tracks: data.tracks.items
}
})
prev = null;
}, function(err) {
console.error(err);
});
}
【问题讨论】:
标签: arrays reactjs jsx spotify