Skip to content

Instantly share code, notes, and snippets.

@gaku-sei
Last active February 6, 2020 10:33
Show Gist options
  • Save gaku-sei/48302a3eb1a5e903624e3979d3f80e32 to your computer and use it in GitHub Desktop.
Save gaku-sei/48302a3eb1a5e903624e3979d3f80e32 to your computer and use it in GitHub Desktop.
Urql useDynamicMutation with Init value
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