oRPC
background

React Query

Simplify React Query usage with minimal integration using ORPC and TanStack Query

Installation

npm install @orpc/client @orpc/react-query @tanstack/react-query

Setup

Using a Global Client

import {  } from '@orpc/react-query';
import {  } from '@orpc/client';
import type {  } from 'examples/server';
 
// Create an ORPC client
export const  = <typeof >({
  : 'http://localhost:3000/api',
});
 
// Create React Query utilities for ORPC
export const  = ();
 
..
  • infiniteOptions
  • key
  • mutationOptions
  • queryOptions

Using Context with a Client

import { ,  } from '@orpc/react-query';
import {  } from '@orpc/client';
import { ,  } from '@tanstack/react-query';
import type {  } from 'examples/server';
import * as React from 'react';
 
const  = React.<<typeof > | undefined>();
 
export function () {
  const  = React.();
 
  if (!) {
    throw new ('ORPCContext is not available.');
  }
 
  return ;
}
 
export function ({  }: { : React. }) {
  const [] = React.(() =>
    <typeof >({
      : 'http://localhost:3000/api',
    })
  );
  const [] = React.(() => new ());
 
  const  = React.(() => (), []);
 
  return (
    < ={}>
      <. ={}>
        {}
      </.>
    </>
  );
}
 
// Example usage
const  = ();
..
  • infiniteOptions
  • key
  • mutationOptions
  • queryOptions

Multiple ORPC Instances

To prevent conflicts when using multiple ORPC instances, you can provide a unique base path to createORPCReactQueryUtils.

import {  } from '@orpc/react-query'
 
// Create separate ORPC instances with unique base paths
const  = ('fake-client' as any, ['__user-api__'])
const  = ('fake-client' as any, ['__post-api__'])

This ensures that each instance manages its own set of query keys and avoids any conflicts.

Usage

Standard Queries and Mutations

import { , , ,  } from '@tanstack/react-query'
import {  } from 'examples/react-query'
 
// Fetch data
const { :  } = (..({ : { : 'unnoq' } }))
 
// Use suspense query
const { :  } = (
  ...({ : { : 'example' } }),
)
 
// Perform mutations
const { :  } = (...())
 
// Invalidate queries
const  = ()
.({ : .() }) // Invalidate all queries
.({ : ...({ : { : 'example' } }) }) // Specific queries

Note: This library enhances TanStack Query by managing query keys and functions for you, providing a seamless developer experience.

Infinite Queries

Infinite queries require a cursor in the input field for pagination.

import {  } from '@orpc/server';
import {  } from 'zod';
import {  } from '@orpc/react-query';
import { ,  } from '@tanstack/react-query';
import * as React from 'react';
 
const  = {
  : {
    : 
      .(.({ : .(), : .() }))
      .(() => ({
        : . + .,
        : [], // Fetch your actual data here
      })),
  },
};
 
const  = <typeof >('fake-client' as any);
 
export function () {
  const  = (
    ...({
      : { : 10 },
      : () => .,
      : 0,
    })
  );
 
  const  = (
    ...({
      : { : 10 },
      : () => .,
      : 0,
    })
  );
 
  return (
    <>
      {. && 'Loading...'}
      {. && <>Data: {.(.)}</>}
      {. && <>Error: {..}</>}
    </>
  );
}

On this page