Skip to content

Instantly share code, notes, and snippets.

@productioncoder productioncoder/App.js
Last active Dec 16, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.