Skip to content

Instantly share code, notes, and snippets.

@sandwichsudo
Last active July 27, 2017 14:32
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 sandwichsudo/05501e0a696f3da3516437dde6a38912 to your computer and use it in GitHub Desktop.
Save sandwichsudo/05501e0a696f3da3516437dde6a38912 to your computer and use it in GitHub Desktop.
Call api when component mounts
// src/containers/RepoSearchPage/page/RepoSearchPage.js
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import RepoResultsList from '../components/RepoResultsList/RepoResultsList';
import * as RepoSearchActions from '../actions/RepoSearchActions';
export class RepoSearchPage extends Component {
constructor(props) {
super(props);
}
componentWillMount() {
this.props.actions.fetchRepos();
}
render() {
return (
<div>
<h1>Top Javascript Repos</h1>
<RepoResultsList repos={this.props.searchResults}/>
</div>
);
}
}
RepoSearchPage.propTypes = {
searchResults: PropTypes.array.isRequired,
actions: PropTypes.object.isRequired,
};
const mapStateToProps = state => ({
searchResults: state.repoSearch.results,
});
const mapDispatchToProps = dispatch => ({
actions: bindActionCreators(RepoSearchActions, dispatch),
});
export default connect(mapStateToProps, mapDispatchToProps)(RepoSearchPage);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment