I assume here that you are trying to play the next song? The way I might try to solve this problem is by passing down another prop, probably a boolean, that determines whether or not the song is played on render. The parent would keep track of which song played last, is playing, or will play next within its state. When your song-end event fires, the parent will call setState and update each of those state values and re-render itself and its children accordingly.
Component(s):
class Group extends React.Component {
constructor(props) {
super(props)
}
state = {
// Initial state. Be careful pressing the back button on the first song :p (Obviously improve this)
previous: -1,
current: 0,
next: 1
}
handleEnd = () => {
// Perform all sorts of condition checks for first song, last song, etc. and code accordingly. This is simplistic, proceeding in linear fashion. You can extrapolate this into other methods for selection of any song, back or forward button, etc.
if (...) {
this.setstate({
previous: previous += 1,
current: current += 1,
next: next +=1
});
}
}
render () {
let songs = this.props.songs;
let songList = songs.map((song) => {
if (song.index === this.state.current) {
return (
<Item playing=1 onEnd={this.handleEnd} mediaUrl={song.url} />
);
}
else {
return (
<Item playing=0 onEnd={this.handleEnd} mediaUrl={song.url} />
);
};
}
return (
<div>
{songList}
</div>
);
}
}
class Item extends React.Component {
constructor(props) {
super(props)
}
componentDidMount = () => {
if (this.props.playing === 1) {
this.play();
};
}
play = () => {
// Play the music from the url or whatever.
}
render () {
<div>
// Some graphical representation of song.
</div>
}
}
Creating a new playlist component:
someSongs = [...];
ReactDOM.render(
<Group songs={someSongs} />,
document.getElementById('...')
);