Last active
March 2, 2021 16:31
-
-
Save alex-boom/641e41edb183545057e25546fa8ad92f to your computer and use it in GitHub Desktop.
custom-hooks
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
// Core | |
import React from 'react'; | |
// Hooks | |
import { useQueryAllAvailablePets } from './hooks/useQueryAllAvailablePets'; | |
export const SpecialList = () => { | |
//pets: data && data.allAvailablePets from custom hooks | |
const { getAllAvailablePets, loading, error, pets } = useQueryAllAvailablePets(); | |
const loaderJSX = loading && ( | |
<p>Loading...</p> | |
); | |
const errorJSX = error && ( | |
<p> | |
We have a problem: {error.message} | |
</p> | |
); | |
const petsJSX = pets && pets.map(({id, name, weight}) => ( | |
<p key={id}> | |
<span>Name: {name}</span> | |
<span>Weight: {weight}</span> | |
</p> | |
)); | |
return ( | |
<> | |
<button onClick={getAllAvailablePets}>Download</button> | |
{loaderJSX} | |
{errorJSX} | |
{petsJSX} | |
</> | |
) | |
}; | |
//custom hooks | |
// Core | |
import { useLazyQuery } from '@apollo/react-hooks'; | |
import { loader } from 'graphql.macro'; | |
// Queries | |
const queryAllAvailablePets = loader('./gql/queryAllAvailablePets.graphql'); | |
export const useQueryAllAvailablePets = () => { | |
const [getAllAvailablePets, { loading, error, data }] = useLazyQuery(queryAllAvailablePets); | |
return { getAllAvailablePets, loading, error, pets: data && data.allAvailablePets } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment