File tree Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Original file line number Diff line number Diff line change 2020- [ SSR Support] ( #ssr-support )
2121- [ Error Handling] ( #error-handling )
2222- [ Dev Tools] ( #devtools )
23+ - [ FAQ] ( #faq )
2324- [ Migrate to v0.8.0] ( #migrate-to-v080 )
2425
2526### Support
@@ -479,6 +480,32 @@ export const Root = () => {
479480}
480481```
481482
483+ ## FAQ
484+
485+ ### atomsWithQuery ` queryKey ` type is always ` unknown ` ?
486+
487+ Explicitly declare the ` get:Getter ` to get proper type inference for ` queryKey ` .
488+
489+ ``` tsx
490+ import { Getter } from ' jotai'
491+
492+ // ❌ Without explicit Getter type, queryKey type might be unknown
493+ const userAtom = atomWithQuery ((get ) => ({
494+ queryKey: [' users' , get (idAtom ).toString ()],
495+ queryFn : async ({ queryKey : [, id ] }) => {
496+ // typeof id = unknown
497+ },
498+ }))
499+
500+ // ✅ With explicit Getter type, queryKey gets proper type inference
501+ const userAtom = atomWithQuery ((get : Getter ) => ({
502+ queryKey: [' users' , get (idAtom ).toString ()],
503+ queryFn : async ({ queryKey : [, id ] }) => {
504+ // typeof id = string
505+ },
506+ }))
507+ ```
508+
482509## Migrate to v0.8.0
483510
484511### Change in atom signature
Original file line number Diff line number Diff line change 1+ import { Getter } from 'jotai'
12import { useAtom } from 'jotai/react'
23import { atom } from 'jotai/vanilla'
34import { atomWithQuery } from 'jotai-tanstack-query'
45
56const idAtom = atom ( 1 )
67
7- const userAtom = atomWithQuery ( ( get ) => ( {
8- queryKey : [ 'users' , get ( idAtom ) ] ,
8+ const userAtom = atomWithQuery ( ( get : Getter ) => ( {
9+ queryKey : [ 'users' , get ( idAtom ) . toString ( ) ] ,
910 queryFn : async ( { queryKey : [ , id ] } ) => {
1011 const res = await fetch ( `https://jsonplaceholder.typicode.com/users/${ id } ` )
1112 return res . json ( )
You can’t perform that action at this time.
0 commit comments