Created
April 15, 2020 17:15
-
-
Save kitze/02d296dca308c674f8b64acf121f5369 to your computer and use it in GitHub Desktop.
returning a hook from a component
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 TrainingView = () => { | |
const trainingSlug = useSingleParam(routeParams.TrainingSlug); | |
const result = useTrainingViewQuery({ | |
variables: { | |
slug: trainingSlug | |
} | |
}); | |
return useHandleData(result)(({ me: { trainingSessions } }) => { | |
let [trainingSession] = trainingSessions; | |
const training = trainingSession?.training; | |
if (!training) { | |
return null; | |
} | |
return ( | |
<A.Vertical p={A.Spacing.XXL} fullW fullH spacing={A.Spacing.L}> | |
<Heading size="lg">{training.name}</Heading> | |
<Heading fontWeight="300" size="md"> | |
{training.course.title} | |
</Heading> | |
) | |
}) | |
}; |
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
export function useHandleData<TData = any, TVariables = any>( | |
result: QueryResult<TData, TVariables> | |
) { | |
return function a(fn: (data: TData | undefined) => any) { | |
const { data, loading, error } = result; | |
if (loading) return <Spinner />; | |
if (error) return <Error error={error.toString()} />; | |
return fn(data); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment