# @trpc-swr/next

> [tRPC](https://trpc.io/)-ified [SWR](https://swr.vercel.app/) hooks

Latest version **2.0.1** (published 2023-10-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install @trpc-swr/next
pnpm add @trpc-swr/next
yarn add @trpc-swr/next
bun add @trpc-swr/next
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2023-10-23 |
| First published | 2023-04-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 207 |
| Author | Sanna Jammeh |
| Maintainers | chiefkoshi |

## Links

- npm: https://www.npmjs.com/package/@trpc-swr/next
- Repository: https://github.com/sannajammeh/trpc-swr
- Homepage: https://trpc-swr.vercel.app
- Issues: https://github.com/sannajammeh/trpc-swr/issues
- npm.io page: https://npm.io/package/@trpc-swr/next

## Recent versions

- 2.0.1 (latest) — 2023-10-23
- 3.0.0-canary.1 (canary) — 2023-12-06
- 1.0.0-rc.0 (rc) — 2023-04-27
- 3.0.0-canary.0 — 2023-12-02
- 2.0.0 — 2023-10-06
- 1.0.2 — 2023-06-24
- 1.0.1 — 2023-05-16
- 1.0.0 — 2023-05-16
- 1.0.0-rc.2 — 2023-05-02
- 1.0.0-rc.1 — 2023-04-28

## README

# @trpc-swr/next

[tRPC](https://trpc.io/)-ified [SWR](https://swr.vercel.app/) hooks

A Next.js helper library for using SWR with tRPC (pages dir).

Documentation: https://trpc-swr.vercel.app/server-side/setup#using-nextjs

## Usage

If you're using Next.js, you can use the `withTRPCSWR` helper function. Then in your pages you must return the `swr` props from `getServerSideProps` or `getStaticProps`.

In `pages/_app.tsx`

```tsx
import { withTRPCSWR } from "@trpc-swr/next";

const MyApp = ({ Component, pageProps }) => {
  return <Component {...pageProps} />;
};

export default withTRPCSWR({})(MyApp);
```

In a page

```tsx
import { createSSG } from "server/ssg";

export default function Page() {
  const { data } = trpc.users.byId.useSWR({ id: 1 });

  return <div>{data!.name}</div>; // data is always defined since it's fetched on the server
}

export const getServerSideProps = () => {
    const trpc = createSSG();

    // You can await this function if you want to wait for the data to be fetched. It's not necessary though.
    trpc.users.byId.fetch({ id: 1 });
    return {
        props: {
            swr: await trpc.dehydrate();
        }
    }
}
```

---
_Source: https://npm.io/package/@trpc-swr/next · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
