T
Join ServertRPC
❓-help
query and mutate promise on next
I've followed the guide "Usage with Next.js" and now I only have the
Edit:
Solution:
useQuery
and use useMutation
hooks exposed when I want to call a route. How do I also expose the regular query
and mutate
promises on these routes?Edit:
Solution:
const utils = trpc.useContext();
// use it (for example in an event handler)
const someResult = await utils.client.someRouter.someProcedure.query(/* args maybe */);
Message Not Public
Sign In & Join Server To View
First instantiating
I'm refering to this part of the docs
mutate
from useMutation
every time seems to be quite inconvenient.I'm refering to this part of the docs
https://trpc.io/docs/quickstart#querying--mutating
. I'd like to call query
and mutate
like described:const user = await trpc.userById.query('1');
const createdUser = await trpc.userCreate.mutate({ name: 'sachinraja' });
3 Messages Not Public
Sign In & Join Server To View
This looks very promising and exactly what I need. I'll try that.
In some cases I think it's more convenient to use the vanilla client instead of hooks, because hooks can not be used everywhere.
Okay, it worked as I needed it to. useContext must still be instantiated, direct access via the vanilla client would be even better, but it works via useContext(). Here is how to use it according to the docs:
const utils = trpc.useContext();
// use it (for example in an event handler)
const someResult = await utils.client.someRouter.someProcedure.query(/* args maybe */);
Thanks @julius and @cje