Created
May 17, 2022 15:17
-
-
Save jth0024/51cc8604f1825f6f5a42f98afb70bb9e to your computer and use it in GitHub Desktop.
A "component hook" that is designed for dependency injection
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 } from 'react'; | |
import { useQuery } from 'react-query'; | |
import type { CharactersApi } from '../apis'; | |
interface UseCharacterListDeps { | |
charactersApi: CharactersApi, | |
} | |
export function useCharacterList(deps: UseCharacterListDeps) { | |
const { charactersApi } = deps | |
const charactersQuery = useQuery('characters', () => charactersApi.list()); | |
const { data: characters, error, loading, refetch } = charactersQuery; | |
const data = { | |
loading, | |
characters, | |
} | |
const handlers = { | |
onRefresh: refetch, | |
} | |
return [data, handlers] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment