Last active
February 6, 2020 10:33
-
-
Save gaku-sei/48302a3eb1a5e903624e3979d3f80e32 to your computer and use it in GitHub Desktop.
Urql useDynamicMutation with Init value
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), | |
}; | |
let use = (~definition) => { | |
let (pristine, setPristine) = React.useState(() => true); | |
let (hasLoaded, setHasLoaded) = React.useState(() => false); | |
let ( | |
{ReasonUrql.Hooks.error, fetching: loading, response}, | |
performMutation, | |
) = | |
ReasonUrql.Hooks.useDynamicMutation(definition); | |
// When the request has been performed yet, but it's loading, and still pristine, | |
// it means the said request just got triggered for the first time, it's not pristine anymore | |
if (!hasLoaded && loading && pristine) { | |
setHasLoaded(const(true)); | |
setPristine(const(false)); | |
}; | |
( | |
{error, loading, response: UrqlExtra.toAsyncResult(~pristine, response)}, | |
performMutation, | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment