JSPM

@trpc/next

11.0.0-rc.824+b21849468
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 254886
  • Score
    100M100P100Q167437F
  • License MIT

The tRPC Next.js library

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@trpc/next) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    tRPC

    tRPC

    End-to-end typesafe APIs made easy

    Demo

    @trpc/next

    Connect a tRPC router to Next.js.

    Documentation

    Full documentation for @trpc/next can be found here

    Installation

    # npm
    npm install @trpc/next@next @trpc/react-query@next @tanstack/react-query
    
    # Yarn
    yarn add @trpc/next@next @trpc/react-query@next @tanstack/react-query
    
    # pnpm
    pnpm add @trpc/next@next @trpc/react-query@next @tanstack/react-query
    
    # Bun
    bun add @trpc/next@next @trpc/react-query@next @tanstack/react-query

    Basic Example

    Setup tRPC in utils/trpc.ts.

    import { createTRPCNext, httpBatchLink } from '@trpc/next';
    // Import the router type from your server file
    import type { AppRouter } from '../pages/api/[trpc].ts';
    
    export const trpc = createTRPCNext<AppRouter>({
      config() {
        return {
          links: [
            httpBatchLink({
              url: 'http://localhost:3000/trpc',
            }),
          ],
        };
      },
      ssr: false,
    });

    Hook up tRPC inside _app.tsx.

    import { trpc } from '~/utils/trpc';
    
    const App = ({ Component, pageProps }) => {
      return <Component {...pageProps} />;
    };
    
    export default trpc.withTRPC(App);

    Now you can query your API in any component.

    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>;
    }

    Server components

    See https://trpc.io/docs/client/react/server-components