Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Class based Container with children props
/*
*
* HackerNews
*
*/
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { compose } from 'redux';
import injectReducer from 'utils/injectReducer';
import injectSaga from 'utils/injectSaga';
import reducer from './reducer';
import saga from './sagas';
import makeSelectHackerNews from './selectors';
import { fetch } from './actions';
class HackerNews extends React.PureComponent {
componentDidMount() {
const { hackerNews } = this.props;
if (!hackerNews.data.length && !hackerNews.fetching) {
this.props.fetch({
offset: 0,
limit: 15,
});
}
}
render() {
const { fetching, data, error } = this.props.hackerNews;
return this.props.children.call(null, {
fetching,
data,
error,
});
}
}
HackerNews.propTypes = {
hackerNews: PropTypes.object.isRequired,
children: PropTypes.func.isRequired,
fetch: PropTypes.func.isRequired,
};
const mapStateToProps = createStructuredSelector({
hackerNews: makeSelectHackerNews(),
});
function mapDispatchToProps(dispatch) {
return {
fetch: (data) => dispatch(fetch(data)),
};
}
const withConnect = connect(
mapStateToProps,
mapDispatchToProps,
);
const withReducer = injectReducer({ key: 'hackerNews', reducer });
const withSaga = injectSaga({ key: 'hackerNews', saga });
export default compose(
withReducer,
withSaga,
withConnect,
)(HackerNews);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment