Skip to content

Instantly share code, notes, and snippets.

@nakedfool
Last active July 31, 2019 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nakedfool/43ff92b683c9aa6708bcdf7acb1e7b4d to your computer and use it in GitHub Desktop.
Save nakedfool/43ff92b683c9aa6708bcdf7acb1e7b4d to your computer and use it in GitHub Desktop.
class Player extends Component {
state = {
isMusicPlaying: false,
data: []
};
componentDidMount() {
axios.defaults.headers.common["Client-ID"] = clientId;
axios
.get("https://api.twitch.tv/helix/streams?user_login=blaschikovsky")
.then(response => {
this.setState({data: response.data})
})
.catch(error => {
console.log(error);
});
}
handleClick = () => {
if (this.state.isMusicPlaying) {
this.audio.pause();
this.audio.setAttribute("src", "");
} else {
this.audio.setAttribute("src", "ip adress");
this.audio.play();
}
this.setState(prevState => {
return {
isMusicPlaying: !prevState.isMusicPlaying
};
});
};
render() {
console.log(this.state.data); // it is pointing out in the state that length of the array is 0 :/ so else is expected
let player;
if (this.state.data.length !== 0) { //
player = (
<div className="PlayerWrapper">
<ReactPlayer
url="https://www.youtube.com/watch?v=ysz5S6PUM-U"
playing
/>
</div>
);
}
else {
player = (
<div className="PlayerWrapper">
<PlayButton
onClick={this.handleClick}
isMusicPlaying={this.state.isMusicPlaying}
/>
<audio
id="audio"
ref={audioTag => {
this.audio = audioTag;
}}
/>
</div>
);
}
return player
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment