peternovak
peternovak15mo ago

Usage of useQuery after pageload?

Hi all! I am new to TRPC so my apologies in advance for a very nooby question: In my [id].tsx file I would like to use the 'id' parameter in my query. Since that takes a fraction of a second to load, I get an unhanded runtime error. What is the proper way to first call my query once the 'id' parameter has been identified, or for that matter later on when the user does something on the page?
12 Replies
Nick
Nick15mo ago
You cannot call a hook conditionally This is a react error
peternovak
peternovak15mo ago
Thanks, yeah I was trying to figure out whether there was some convenient trpc workaround. All I want is to pass in the [id] from the slug of the page to my trpc query.
Nick
Nick15mo ago
useQuery has an enabled:boolean flag
peternovak
peternovak15mo ago
Thanks, let me have a look!
peternovak
peternovak15mo ago
Getting the same error despite trying this. Am I missing something?
Nick
Nick15mo ago
Can you share the rest of the component?
peternovak
peternovak15mo ago
import type { NextPage } from "next"; import Image from "next/image"; import { useSession } from "next-auth/react"; import { useRouter } from "next/router"; import { api } from "@/utils/api"; import { LoadingSpinner } from "@/components/loading"; const EvaluationPage: NextPage = () => { const { data: session } = useSession(); const router = useRouter(); const { id } = router.query; if (!id || Array.isArray(id)) return <LoadingSpinner />; const { data } = api.image.getEvaluation.useQuery( { id, }, { enabled: false, } ); return ( <> <main className="flex min-h-screen flex-col items-center justify-center bg-gradient-to-br from-blue-400 to-indigo-800"> {session ? ( <div className="container flex flex-col items-center justify-center gap-12 px-4 py-12 "> <h1 className="font-base text-3xl tracking-tight text-gray-100 sm:text-3xl"> Image evaluation </h1> <>{data && <p className="text-white">{data.response}</p>}</> </div> ) : ( <div className="flex flex-col items-center justify-center gap-4 py-4"> <h3 className="text-center text-2xl text-gray-100"> Please sign in to continue. </h3> </div> )} </main> </> ); };
peternovak
peternovak15mo ago
peternovak
peternovak15mo ago
Could it be that TRPC does not pick up the disabled setting like in this post? https://stackoverflow.com/questions/73940139/trpc-doesnt-react-to-config-variables-passed-e-g-enabled-false
Stack Overflow
TRPC doesn't react to config / variables passed (e.g.: enabled : fa...
New to trpc and am trying to understand how to use useQuery (which I am familiar with from react-query): const IndexPage = () => { const { isLoading, data, isIdle } = trpc.useQuery([ "
caalyx
caalyx15mo ago
you still have the hook called conditionally just fyi all hooks must be before any return statements
Nick
Nick15mo ago
Above ^
peternovak
peternovak15mo ago
Wow, that was it! Thanks a lot guys! 🙏 I removed the return statement and everything just worked