tRPCttRPC
Powered by
functiondjF
tRPC•2y ago•
24 replies
functiondj

Why are `new QueryClient` and `trpc.createClient` run inside a component in the React setup?

From https://trpc.io/docs/client/react/setup:
function App() {
  const [queryClient] = useState(() => new QueryClient());
  const [trpcClient] = useState(() =>
    trpc.createClient({ ... }),
  );
  // ...
function App() {
  const [queryClient] = useState(() => new QueryClient());
  const [trpcClient] = useState(() =>
    trpc.createClient({ ... }),
  );
  // ...

if these two are "made stable" through useState() and since they should exist only once per SPA (like a singleton), why not just put them outside the component?
is this related to HMR?
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

Typing on trpc.createClient?
hashwarpHhashwarp / ❓-help
17mo ago
Why not create queryClient and trpcClient outside of React?
functiondjFfunctiondj / ❓-help
2y ago
Test React component using trpc client and `useSuspenseQuery`
playmPplaym / ❓-help
2y ago