This file contains hidden or 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
| // https://tanstack.com/query/v4/docs/framework/react/overview | |
| const { data, isLoading, isError } = useQuery({ | |
| queryKey: ['repoData'], | |
| queryFn: () => { | |
| return fetch('https://api.github.com/repos/TanStack/query').then((res) => res.json()) | |
| }, | |
| }); |
This file contains hidden or 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
| interface User { | |
| name: string; | |
| } | |
| const api = { | |
| getUser: (userId: number) => { | |
| return new Promise<User>((resolve) => { | |
| resolve({ | |
| name: 'Elan', | |
| }); |
This file contains hidden or 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
| // simplified | |
| function useApiQuery(endpointName, params, options) { | |
| const { onError, ...restOptions } = options ?? {}; | |
| return useQuery({ | |
| queryKey: [endpointName, ...params], | |
| queryFn: () => { | |
| return api[endpointName].apply(null, params); | |
| }, | |
| onError: (e) => { |
This file contains hidden or 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
| const { data: queryData } = useQuery({ | |
| queryKey: ['getUser', userId], | |
| queryFn: () => api.getUser(userId), | |
| onError: (e) => { | |
| alertErrorMonitoring(e); | |
| }, | |
| }); | |
| // vs |
This file contains hidden or 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
| interface User { | |
| name: string; | |
| } | |
| // the fake API SDK from earlier | |
| const api = { | |
| getUser: (userId: number) => { | |
| return new Promise<User>((resolve) => { | |
| resolve({ | |
| name: 'Elan', |
This file contains hidden or 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
| type UseApiQueryOptions<EndpointName extends ApiEndpointName, Prefetched extends boolean | undefined> = UseQueryOptions< | |
| ApiResponse<EndpointName> | |
| > & { | |
| prefetched?: Prefetched; | |
| }; | |
| type UseApiQueryData<EndpointName extends ApiEndpointName, Prefetched extends boolean | undefined> = Prefetched extends true | |
| ? ApiResponse<EndpointName> | |
| : ApiResponse<EndpointName> | undefined; |
This file contains hidden or 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
| // simplified | |
| export function AsyncStatus({ | |
| children, | |
| customErrorState, | |
| isError, | |
| isLoading, | |
| loadingState | |
| }) { | |
| if (isLoading) { | |
| return loadingState; |
This file contains hidden or 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
| function Component() { | |
| const { data, isLoading, isError } = useApiQuery('getUser', [userId]); | |
| function renderContent() { | |
| // can assume data is populated in this function! | |
| } | |
| return ( | |
| <AsyncStatus | |
| isLoading={isLoading} |
This file contains hidden or 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
| <FetchApiQuery | |
| endpointName="getUser" | |
| params={[userId]} | |
| loadingState={<Spinner />} | |
| > | |
| {({ data }) => { | |
| // data is populated in this function! | |
| }} | |
| </FetchApiQuery> |
This file contains hidden or 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
| function Component() { | |
| const { data, isLoading, isError } = useApiQuery('getUser', [userId], { suspense: true }); | |
| // can assume data is populated at the top level! | |
| return /* ... */ | |
| } | |
| function Wrapper() { | |
| return ( |