npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

trpc-react

v0.1.0-beta.12

Published

Standalone TRPC React package reimplementing `@trpc/react-query` in a less opinionated way. May eventually be merged into the official `@trpc/react-query` package.

Downloads

10

Readme

TRPC React

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

Motivation

While trying to integrate TRPC into my React app, I

API

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.

Create a Client

const trpc = createTRPCReact<AppRouter>({
  config: {
    links: [
      /*...*/
    ],
    transformer: SuperJSON,
  },
});

Create Query Provider

// _app.tsx or root file
const queryClient = useState(() => new QueryClient());

return <QueryProvider>{children}</QueryProvider>;

Use hooks

Use Query

const query = trpc.some.procedure.useQuery({
  /* Input */
});

Use Infinite Query

const query = trpc.some.procedure.useInfiniteQuery({
  /* Input */
});

Use Lazy Query

const [fetch, query] = trpc.some.procedure.useLazyQuery({
  /* Options */
});

Use Mutation

const mut = trpc.some.procedure.useMutation({
  /* Options */
});

Use non-hooks

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(/*...*/);

Hydration helpers

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

Create Server-Side Direct Client

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.