Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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