Skip to content

Instantly share code, notes, and snippets.

@productioncoder
Last active December 16, 2018 16:14
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/a27f91612a1057e4c0554fbae1eca657 to your computer and use it in GitHub Desktop.
Save productioncoder/a27f91612a1057e4c0554fbae1eca657 to your computer and use it in GitHub Desktop.
Fetching most popular videos in Home component
import Home from './containers/Home/Home';
/*
Rest unchanged
*/
import React from 'react';
import {connect} from "react-redux";
import * as videoActions from "../../store/actions/video";
import {bindActionCreators} from 'redux';
import {getYoutubeLibraryLoaded} from '../../store/reducers/api';
/* ... */
class Home extends React.Component {
/* ... */
componentDidMount() {
if (this.props.youtubeLibraryLoaded) {
this.props.fetchMostPopularVideos();
}
}
componentDidUpdate(prevProps) {
if (this.props.youtubeLibraryLoaded !== prevProps.youtubeLibraryLoaded) {
this.props.fetchMostPopularVideos();
}
}
}
function mapStateToProps(state) {
return {
youtubeLibraryLoaded: getYoutubeLibraryLoaded(state),
};
}
function mapDispatchToProps(dispatch) {
const fetchMostPopularVideos = videoActions.mostPopular.request;
return bindActionCreators({fetchMostPopularVideos}, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(Home);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment