-
-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* docs: add trpc integration * fix typo
- Loading branch information
Showing
1 changed file
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
--- | ||
title: tRPC | ||
description: This doc describes tRPC integration. | ||
nav: 4.10 | ||
--- | ||
|
||
You can use Jotai with [tRPC](https://trpc.io). | ||
|
||
## Install | ||
|
||
You have to install `jotai-trpc`, `@trpc/client` and `@trpc/server` to use the integration. | ||
|
||
``` | ||
yarn add jotai-trpc @trpc/client @trpc/server | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
import { createTRPCJotai } from 'jotai-trpc' | ||
|
||
const trpc = | ||
createTRPCJotai < | ||
MyRouter > | ||
{ | ||
links: [ | ||
httpLink({ | ||
url: myUrl, | ||
}), | ||
], | ||
} | ||
|
||
const idAtom = atom('foo') | ||
const queryAtom = trpc.bar.baz.atomWithQuery((get) => get(idAtom)) | ||
``` | ||
|
||
## atomWithQuery | ||
|
||
`...atomWithQuery` creates a new atom with "query". It internally uses [Vanilla Client](https://trpc.io/docs/vanilla)'s `...query` procedure. | ||
|
||
```jsx | ||
import { atom, useAtom } from 'jotai' | ||
import { httpLink } from '@trpc/client' | ||
import { createTRPCJotai } from 'jotai-trpc' | ||
import { trpcPokemonUrl } from 'trpc-pokemon' | ||
import type { PokemonRouter } from 'trpc-pokemon' | ||
|
||
const trpc = | ||
createTRPCJotai < | ||
PokemonRouter > | ||
{ | ||
links: [ | ||
httpLink({ | ||
url: trpcPokemonUrl, | ||
}), | ||
], | ||
} | ||
|
||
const NAMES = [ | ||
'bulbasaur', | ||
'ivysaur', | ||
'venusaur', | ||
'charmander', | ||
'charmeleon', | ||
'charizard', | ||
'squirtle', | ||
'wartortle', | ||
'blastoise', | ||
] | ||
|
||
const nameAtom = atom(NAMES[0]) | ||
|
||
const pokemonAtom = trpc.pokemon.byId.atomWithQuery((get) => get(nameAtom)) | ||
|
||
const Pokemon = () => { | ||
const [data] = useAtom(pokemonAtom) | ||
return ( | ||
<div> | ||
<div>ID: {data.id}</div> | ||
<div>Height: {data.height}</div> | ||
<div>Weight: {data.weight}</div> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
### Examples | ||
|
||
<CodeSandbox id="j1vhcg" /> | ||
|
||
## atomWithMutation | ||
|
||
`...atomWithMutation` creates a new atom with "mutate". It internally uses [Vanilla Client](https://trpc.io/docs/vanilla)'s `...mutate` procedure. | ||
|
||
FIXME: add code example and codesandbox | ||
|
||
## atomWithSubscription | ||
|
||
`...atomWithSubscription` creates a new atom with "subscribe". It internally uses [Vanilla Client](https://trpc.io/docs/vanilla)'s `...subscribe` procedure. | ||
|
||
FIXME: add code example and codesandbox |
3930a2a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
jotai – ./
jotai-pmndrs.vercel.app
jotai.org
jotai.vercel.app
jotai-website.vercel.app
www.jotai.org
jotai-git-main-pmndrs.vercel.app
jotai.pmnd.rs