Skip to content

Instantly share code, notes, and snippets.

@gaku-sei
Created February 7, 2020 06:30
Show Gist options
  • Save gaku-sei/274781656fdad151ac41ce6161fec087 to your computer and use it in GitHub Desktop.
Save gaku-sei/274781656fdad151ac41ce6161fec087 to your computer and use it in GitHub Desktop.
Urql with Init value hook
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