Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
8 replies
Shoodey

How to make a trpc request wait on another hook

I basically have a state that is populated from a query param then an API call (i used context before and now jotai) but it is critical to populate that state first before issuing a following trpc call that will fetch something.

// store.ts
export const currentGuildAtom = atom<DiscordGuildProfile | null>(null);

// page.tsx
const guild = useAtomValue(currentGuildAtom);

const { data: textChannels } =
  trpc.guildChannels.getGuildTextChannels.useQuery({
    guildId: guild.id,
    });


I need to initialize the state at null since it could actually be that where i manage that case by redirecting elsewhere. but if I hit this page, the state is populated but too early for trpc to handle it, and since hooks cannot live inside useEffect, I dont see a way to handle this, is there a way to suspend the trpc hook until I validate that guild is not null? or another workaround?
Was this page helpful?