Skip to content

Instantly share code, notes, and snippets.

@AllGistsEqual
Created February 22, 2021 20:04
Show Gist options
  • Save AllGistsEqual/c9aa3df7c7e8eeec5ada1569487174e9 to your computer and use it in GitHub Desktop.
Save AllGistsEqual/c9aa3df7c7e8eeec5ada1569487174e9 to your computer and use it in GitHub Desktop.
// File: src/components/pages/articles.hooks.ts
import { useEffect } from 'react'
import { requestArticlesData, Article } from '../../redux/ducks/articles'
import { useReduxDispatch, useReduxSelector } from '../../redux'
export const useArticlesData = (): Article[] | null => {
const data = useReduxSelector(state => {
return state.articles.data || null
})
const dispatch = useReduxDispatch()
useEffect(() => {
if (!data) {
dispatch(requestArticlesData())
}
}, [dispatch, data])
return data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment