Last active
May 17, 2022 14:49
-
-
Save jth0024/d89d3e147b7abb1186b901a7682bbe12 to your computer and use it in GitHub Desktop.
A "Component Hook" example
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
import { useCallback, useEffect } from 'react'; | |
import { useDispatch, useSelector } from 'react-redux'; | |
export function useCharacterList() { | |
const characters = useSelector(characterListSelector); | |
const loading = useSelector(characterListLoadingSelector); | |
const dispatch = useDispatch(); | |
const fetchCharacters = useCallback(() => { | |
dispatch(actions.fetchCharacters(); | |
}, [dispatch]) | |
useEffect(() => { | |
dispatch(actions.fetchCharacters()); | |
}, []) | |
const data = { | |
loading, | |
characters, | |
} | |
const handlers = { | |
onRefresh: fetchCharacters, | |
} | |
return [data, handlers] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment