
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
@trpc/react-query
Advanced tools
End-to-end typesafe APIs made easy
@trpc/react-queryA tRPC wrapper around react-query.
Full documentation for @trpc/react-query can be found here
# npm
npm install @trpc/react-query @tanstack/react-query
# Yarn
yarn add @trpc/react-query @tanstack/react-query
# pnpm
pnpm add @trpc/react-query @tanstack/react-query
# Bun
bun add @trpc/react-query @tanstack/react-query
Create a utils file that exports tRPC hooks and providers.
import { createTRPCReact } from '@trpc/react-query';
import type { AppRouter } from './server';
export const trpc = createTRPCReact<AppRouter>();
Use the provider to connect to your API.
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { trpc } from '~/utils/trpc';
import React, { useState } from 'react';
export function App() {
const [queryClient] = useState(() => new QueryClient());
const [trpcClient] = useState(() =>
trpc.createClient({
url: 'http://localhost:5000/trpc',
}),
);
return (
<trpc.Provider client={trpcClient} queryClient={queryClient}>
<QueryClientProvider client={queryClient}>
{/* Your app here */}
</QueryClientProvider>
</trpc.Provider>
);
}
Now in any component, you can query your API using the proxy exported from the utils file.
import { trpc } from '~/utils/trpc';
export function Hello() {
const { data, error, status } = trpc.greeting.useQuery({ name: 'tRPC' });
if (error) {
return <p>{error.message}</p>;
}
if (status !== 'success') {
return <p>Loading...</p>;
}
return <div>{data && <p>{data.greeting}</p>}</div>;
}
React Query is a powerful data-fetching library for React applications. It provides hooks for fetching, caching, and synchronizing server data with client-side state. Unlike @trpc/react-query, it does not provide built-in type safety or integration with tRPC.
Apollo Client is a comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL. It offers features like caching, optimistic UI, and subscriptions. Unlike @trpc/react-query, it uses GraphQL instead of tRPC for API interactions.
SWR (stale-while-revalidate) is a React Hooks library for data fetching developed by Vercel. It focuses on simplicity and performance, providing a lightweight solution for fetching and caching data. Unlike @trpc/react-query, it does not offer built-in type safety or integration with tRPC.
FAQs
The tRPC React library
The npm package @trpc/react-query receives a total of 642,478 weekly downloads. As such, @trpc/react-query popularity was classified as popular.
We found that @trpc/react-query demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers 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
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.