Skip to content

Instantly share code, notes, and snippets.

@productioncoder
Last active December 17, 2018 16:05
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 productioncoder/044baeb8e6dc628d6c8a3d044a628f09 to your computer and use it in GitHub Desktop.
Save productioncoder/044baeb8e6dc628d6c8a3d044a628f09 to your computer and use it in GitHub Desktop.
Youtube Home component fetch most popular videos by category id
/*....*/
class Home extends React.Component {
constructor(props) {
super(props);
this.state = {
categoryIndex: 0,
};
}
componentDidUpdate(prevProps) {
if (this.props.youtubeLibraryLoaded !== prevProps.youtubeLibraryLoaded) {
this.fetchCategoriesAndMostPopularVideos();
} else if (this.props.videoCategories !== prevProps.videoCategories) {
this.fetchVideosByCategory();
}
}
fetchVideosByCategory() {
const categoryStartIndex = this.state.categoryIndex;
const categories = this.props.videoCategories.slice(categoryStartIndex, categoryStartIndex + 3);
this.props.fetchMostPopularVideosByCategory(categories);
this.setState(prevState => {
return {
categoryIndex: prevState.categoryIndex + 3,
};
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment