'use client' with tRPC? Type error: This expression is not callable
From boiler
npx create-t3-app@latest
using the app router:
On simple tRPC file src/server/api/routers/test.ts
:
On src/server/api/root.ts
:
And when calling it from src/app/page.tsx
I have no issues at all. But when I do call it from a component in _components
folder that has a 'use client' directive it gives me this error. I don't understand since it is the same code replication already available on the boilerplate on the _components/create-post.tsx
. So basically:
Error:
10 Replies
I am not sure if I am not following a best practices
Solution
api.test.getCountryList.query()
I think you also may need to await it aswell?
Damn, I'm so stupid. This is the correct code:
Good question, testing it. I thinkg if I do not await it ts won't know the types later on to do for example a .map ?
I haven't used trpc with react so i don't know how you do stuff with that
but with the await it is complaining:
Unexpected
await
of a non-Promise (non-"Thenable") value. [@typescript-eslint/await-thenable]
'await' has no effect on the type of this expressionIt looks like this is correct for trpc react query i think
ok, just trpc is sending the result inside the
data
value of the object so the correct way is:
So destructuring it at first and then good to go! 🙂useQuery() | tRPC
The hooks provided by @trpc/react-query are a thin wrapper around @tanstack/react-query. For in-depth information about options and usage patterns, refer to their docs on queries.
Man.. I'm so rusty on react 😓