Skip to content

Instantly share code, notes, and snippets.

@jth0024
Last active May 17, 2022 14:49
Show Gist options
  • Save jth0024/d89d3e147b7abb1186b901a7682bbe12 to your computer and use it in GitHub Desktop.
Save jth0024/d89d3e147b7abb1186b901a7682bbe12 to your computer and use it in GitHub Desktop.
A "Component Hook" example
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