
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
trpc-react
Advanced tools
Standalone TRPC React package reimplementing `@trpc/react-query` in a less opinionated way. May eventually be merged into the official `@trpc/react-query` package.
Standalone TRPC React package reimplementing @trpc/react-query in a less opinionated way. May eventually be merged into the official @trpc/react-query package.
pnpm add trpc-react
Features:
🛠️ Makes integrating TRPC into existing RQ applications seamless
🔄 Useful helper methods like getting key, fetcher functions and prefetching exposed.
🕸️ SSG not restricted to direct server-side calls
🍳 Simpler interface
🔌 Plugs into React Query instead of encapsulating and obscuring functionality
🧑🦯 Agnostic to meta-framework and backend setup
While trying to integrate TRPC into my React app, I
The API consists of client-side only hooks and various helpers which are particularly useful for integrating with React Query in an idiomatic way.
The API aims to be a superset, and thus drop-in replacement, of the @trpc/react-query.
const trpc = createTRPCReact<AppRouter>({
config: {
links: [
/*...*/
],
transformer: SuperJSON,
},
});
// _app.tsx or root file
const queryClient = useState(() => new QueryClient());
return <QueryProvider>{children}</QueryProvider>;
const query = trpc.some.procedure.useQuery({
/* Input */
});
const query = trpc.some.procedure.useInfiniteQuery({
/* Input */
});
const [fetch, query] = trpc.some.procedure.useLazyQuery({
/* Options */
});
const mut = trpc.some.procedure.useMutation({
/* Options */
});
Unlike React Hooks, the below can be used anywhere in your app, especially in SSR and SSG use-cases.
// Pre-fetch and return data, adding to queryClient cache
trpc.some.procedure.$fetch(queryClient, {
/* Input */
});
// Pre-fetch data, adding to queryClient cache. No data returned or errors thrown
trpc.some.procedure.$prefetch(queryClient, {
/* Input */
});
// Call raw mutation. No affect on cache
trpc.some.procedure.$mutate(/* Input */);
// Call raw query. No affect on cache
trpc.some.procedure.$query(/* Input */);
trpc.some.procedure.$prefetchInfinite(/*...*/);
trpc.some.procedure.$fetchInfinite(/*...*/);
In the trpc docs, they describe how to achieve SSG, but only via direct server-side calls. This means if your tRPC server isn't deployed alongside your frontend static rendering runtime (e.g. getStaticProps) then SSG isn't straightforward nor documented.
Unlike the official @trpc/react-query package, trpc-react plugs into the existing idimiomatic RQ approach. However the TRPC query data requires special serialization, so the below should be drop-in replaced for existing dehydrate calls made via RQ.
export const getStaticProps = () => {
const queryClient = new QueryClient();
// Prefetch any data
return {
props: {
dehydratedState: trpc.$dehydrate(queryClient),
},
};
};
In _app.tsx:
// Pre-hydrate State de-serializes the data, if necessary.
// This is backwards compatible with vanilla RQ dehydratedState
const prehydrateState = trpc.$prehydrate(pageProps.dehydratedState);
return (
<QueryProvider queryClient={queryClient}>
<Hydrate state={prehydrateState}></Hydrate>
</QueryProvider>
);
To achieve the same direct-server-side call functionality as TRPC, we can create a special server client. Under the hood, this client calls procedures directly, and hooks are therefore disabled.
const trpcServer = createTRPCReactServer({
appRouter, // <- Your router
})
trpcServer.some.procedure.$fetch(/*...*/)
// ... etc.
FAQs
Standalone TRPC React package reimplementing `@trpc/react-query` in a less opinionated way. May eventually be merged into the official `@trpc/react-query` package.
We found that trpc-react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.