Skip to content

Instantly share code, notes, and snippets.

@zsajjad
Created July 1, 2019 22:35
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 zsajjad/478d5bb1cc579f9d6ecead0327531c9d to your computer and use it in GitHub Desktop.
Save zsajjad/478d5bb1cc579f9d6ecead0327531c9d to your computer and use it in GitHub Desktop.
Hook based Container with children props
/*
*
* HackerNews
*
*/
import React from 'react';
import PropTypes from 'prop-types';
import { useSelector, useDispatch, shallowEqual } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import reducer from './reducer';
import saga from './sagas';
import makeSelectHackerNews from './selectors';
import { fetch } from './actions';
function useHackerNews(props) {
const hackerNews = useSelector(makeSelectHackerNews, shallowEqual);
const dispatch = useDispatch();
useEffect(() => {
if (!hackerNews.data.length && !hackerNews.fetching) {
dispatch(fetch({
offset: 0,
limit: 15,
}));
}
}, [hackerNews]);
return hackerNews;
}
export default function HackerNews({ children, ...props }) {
const hackerNews = useHackerNews(props);
return children(hackerNews);
};
HackerNews.propTypes = {
children: PropTypes.func.isRequired,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment