tRPCttRPC
Powered by
BaboluoB
tRPC•3y ago•
15 replies
Baboluo

How to do an async API call in useEffect (T3 stack)

Hey, I have the router below and want to call the
tutor
tutor
async in an
useCallback
useCallback
function, but the only method that is available is
api.chat.queryTutor.useQuery()
api.chat.queryTutor.useQuery()
which is a hook where you add the input in advance. But I have it available only in the
useCallback
useCallback
function. is there a way to access the function directly without a hook?

export const chatRouter = createTRPCRouter({
  tutor: publicProcedure
    .input(
      z.object({
        messages: z.array(
          z.object({
            id: z.string(),
            text: z.string(),
            role: z.enum(["user", "system", "assistant"]),
            addToPrompt: z.boolean(),
          })
        ),
      })
    )
    .query(async ({ input }): Promise<ChatMessage> => {
      // ...
      return {
        // ...
      }
    }),
})
export const chatRouter = createTRPCRouter({
  tutor: publicProcedure
    .input(
      z.object({
        messages: z.array(
          z.object({
            id: z.string(),
            text: z.string(),
            role: z.enum(["user", "system", "assistant"]),
            addToPrompt: z.boolean(),
          })
        ),
      })
    )
    .query(async ({ input }): Promise<ChatMessage> => {
      // ...
      return {
        // ...
      }
    }),
})
Solution
You are after all getting a new state back from the request, the backend is creating something for you even if it doesn't store it
Jump to solution
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

T3 Stack TRPC used in Server Actions
JonathanJJonathan / ❓-help
3y ago
TRPC Vanilla Client (T3 stack)
meowMmeow / ❓-help
3y ago
Change db url in ctx with an API call
NicoFishNNicoFish / ❓-help
2y ago
how to connect trpc server (t3 stack) from my external nextjs app?
gimbledevGgimbledev / ❓-help
3y ago