Skip to content

Instantly share code, notes, and snippets.

@webdeb
Created May 31, 2022 17:36
Show Gist options
  • Save webdeb/023906bcd920453d2adf730871561c30 to your computer and use it in GitHub Desktop.
Save webdeb/023906bcd920453d2adf730871561c30 to your computer and use it in GitHub Desktop.
useLazyQuery for Blitz.js
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