Created
July 1, 2019 22:35
-
-
Save zsajjad/478d5bb1cc579f9d6ecead0327531c9d to your computer and use it in GitHub Desktop.
Hook based Container with children props
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* 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