Created
May 31, 2022 17:36
-
-
Save webdeb/023906bcd920453d2adf730871561c30 to your computer and use it in GitHub Desktop.
useLazyQuery for Blitz.js
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
import { useState } from "react" | |
import { useQuery } from "blitz" | |
const useLazyQuery = (resolver, inputArg, options = {}) => { | |
const [enabled, setEnabled] = useState(false) | |
const [result, extras] = useQuery(resolver, inputArg, { | |
...options, | |
enabled, | |
suspense: false, | |
onSettled: () => setEnabled(false), | |
}) | |
const execute = () => setEnabled(true) | |
return [result, { ...extras, execute }] | |
} | |
export default useLazyQuery | |
/** | |
Usage: | |
const [data, {execute}] = useLazyQuery(resolver, null) | |
<button onClick={execute}>Load data</button> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment