tRPCttRPC
Powered by
BlitzB
tRPC•3y ago•
4 replies
Blitz

avoiding used more hooks than previous render

I understand whats causing the error in my code, im checking if id is defined before calling useQuery as it requires it to be passed in.

const Update = () => {
    const router = useRouter()
    const { id } = router.query
    if (!id) return <>Invalid ID</>
    
    const readOneQuery = procedures.readOne.useQuery({ id: id as string })
    
    if (readOneQuery.isLoading) return <ScreenLoader />
    if (readOneQuery.isError) return <>Something went wrong while fetching data</>


    return <FormBuilder type="update" model={model} procedures={procedures} currentId={id as string} prefill={readOneQuery.data} />

}
const Update = () => {
    const router = useRouter()
    const { id } = router.query
    if (!id) return <>Invalid ID</>
    
    const readOneQuery = procedures.readOne.useQuery({ id: id as string })
    
    if (readOneQuery.isLoading) return <ScreenLoader />
    if (readOneQuery.isError) return <>Something went wrong while fetching data</>


    return <FormBuilder type="update" model={model} procedures={procedures} currentId={id as string} prefill={readOneQuery.data} />

}


How do i make sure useQuery is only called when is becomes defined, but in the same time calling it before the null check of id?
Solution

Solution

pass
enabled: condition
enabled: condition
in useQuery options

procedures.readOne.useQuery({ id: id as string }, { enabled: id !== undefined })
procedures.readOne.useQuery({ id: id as string }, { enabled: id !== undefined })
Jump to solution
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

keep previous data
shikishikichangchangSshikishikichangchang / ❓-help
2y ago
Custom hooks v11
BEEIRLBBEEIRL / ❓-help
9mo ago
useEffect and useMutation error about conditional rendering of hooks
𝛈𝞂𝗿𝞈𝒆𝒈𝖏𝝰𝛈 𝐜0𝟃3𝗿�𝛈𝞂𝗿𝞈𝒆𝒈𝖏𝝰𝛈 𝐜0𝟃3𝗿 / ❓-help
3y ago