Created
February 7, 2020 06:30
-
-
Save gaku-sei/274781656fdad151ac41ce6161fec087 to your computer and use it in GitHub Desktop.
Urql with Init value hook
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
type t('a) = { | |
error: option(UrqlCombinedError.t), | |
loading: bool, | |
response: AsyncResult.t('a, UrqlCombinedError.t), | |
}; | |
// request here will be the first value returned by the current hooks | |
let use = (~request) => { | |
let (pristine, setPristine) = React.useState(() => true); | |
let (hasLoaded, setHasLoaded) = React.useState(() => false); | |
let ({ReasonUrql.Hooks.error, fetching: loading, response}, performRequest) = request; | |
if (!hasLoaded && loading && pristine) { | |
setHasLoaded(_ => true); | |
setPristine(_ => false); | |
}; | |
( | |
{error, loading, response: convertToResponseWithInit(~pristine, response)}, | |
performRequest, | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment