oRPC
background

React

Fully typed client for React JS with TanStack Query

Installation

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

Setup

import {  } from '@orpc/react'
import {  } from '@orpc/client'
import { ,  } from '@tanstack/react-query'
import {  } from 'react'
import type {  } from 'examples/server'
import * as React from 'react'
 
export const { ,  } = <typeof  /** or contract router */>()
 
export function ({  }: { : React. }) {
    const [] = (() => <typeof  /** must match with createORPCReact*/>({
        : 'http://localhost:3000/api',
    }))
    const [] = (() => new ())
 
    return (
        < ={}>
            <. ={{ ,  }}>
                {/* If you has own QueryClientProvider please put it here*/}
                {}
            </.>
        </>
    )
}

The client is exactly the same as the vanilla client. You can create it globally or in a component based on your needs.

Usage

import {  } from 'examples/react'
 
const { :  } = ..({ : 'unnoq' })
const { :  } = ...({ : 'example' })
const { :  } = ...()
 
...
  • useInfiniteQuery
  • useIsFetching
  • useIsMutating
  • useMutation
  • useMutationState
  • usePrefetchInfiniteQuery
  • usePrefetchQuery
  • useQuery
  • useSuspenseInfiniteQuery
  • useSuspenseQuery

useMutation and other APIs are exactly the same; we just wrap TanStack Query. However, oRPC will manage the query key and function for you, providing the best developer experience.

Use Infinite Query

To use useInfiniteQuery, the procedure must have a cursor input field (with any type).

import {  } from '@orpc/server'
import {  } from 'zod'
import * as React from 'react'
import {  } from '@orpc/react'
 
const  = {
    : {
        : 
        .(.({ : .(), : .() }))
        .(() => {
            return {
                : . + .,
                : []
            }
        })
    }
}
 
const {  } = <typeof >()
 
export function () {
    const  = ...(
         {
            : { : 10 },
            : () => .,
            : 0,
        })
 
    return (
        <>
            {. === 'pending' && 'pending...'}
            {. === 'success' && <>Data: {.(.)}</>}
            {. === 'error' && <>Error: {..}</>}
        </>
    )
}

Again, oRPC useInfiniteQuery just wraps TanStack Query. However, oRPC will manage the query key and function for you, providing the best developer experience.

Utils

import {  } from 'examples/react'
 
const  = .()
.() // invalidate all queries
const  = await ..({ : 'unnoq' })
 
...
  • cancel
  • ensureInfiniteQueryData
  • ensureQueryData
  • fetchInfiniteQuery
  • fetchQuery
  • getInfiniteQueriesData
  • getInfiniteQueryData
  • getInfiniteQueryDefaults
  • getInfiniteQueryState
  • getMutationDefaults
  • getQueriesData
  • getQueryData
  • getQueryDefaults
  • getQueryState
  • invalidate
  • isFetching
  • isMutating
  • prefetchInfiniteQuery
  • prefetchQuery
  • refetch
  • remove
  • reset
  • setInfiniteQueriesData
  • setInfiniteQueryData
  • setInfiniteQueryDefaults
  • setMutationDefaults
  • setQueriesData
  • setQueryData
  • setQueryDefaults

Use Queries

import {  } from 'examples/react'
 
const  = .( => [
  .({ : 'unnoq' }), 
  ..({ : 'example' })
])

Use Context

import {  } from 'examples/react'
 
const {, } = .()
 
const  = await .({ : 'unnoq' })

Others

oRPC React is well tested, it provides up to 30+ wrapper TanStack Query APIs. Please install and tinker with it.

On this page