実現したいこと
以下のエラーを解決したいです。
現在エラー原因は、useQuery関数の戻り値である左辺のプロパティの型に問題があると推測しています。
サイトで調べましたが修正箇所が把握できておりません。
ご存知の方、問題のある箇所を御指摘して頂きたく思います。
宜しくお願いします。
発生している問題・分からないこと
●エラー内容
Bad argument type. Starting with v5, only the "Object" form is allowed when calling query related functions. Please use the error stack to find the culprit call. More info here: https://tanstack.com/query/latest/docs/react/guides/migrating-to-v5#supports-a-single-signature-one-object
該当のソースコード
index.js
1import React from 'react'; 2import ReactDOM from 'react-dom/client'; 3import './index.css'; 4import reportWebVitals from './reportWebVitals'; 5import QueryBasic from './practiceFiles/QueryBasic.js'; 6import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; 7 8const root = ReactDOM.createRoot(document.getElementById('root')); 9 10const que = new QueryClient(); 11root.render( 12 <QueryClientProvider client={que}> 13 <QueryBasic /> 14 </QueryClientProvider> 15)
QueryBasic.js
1import { useQuery } from "@tanstack/react-query"; 2 3// 2000ミリ秒、処理を停止させる関数 4const sleep = delay => new Promise(resolve => setTimeout(resolve, delay)); 5 6// 天気情報を取得する関数 7const fetchWeather = async () => { 8 // 意図的に処理を停止させる関数 9 await sleep(2000); 10 const res = await fetch("https://api.openweathermap.org/data/2.5/weather?q=Tokyo&appid=〇〇"); 11 12 // 非同期処理が成功 13 if(res.ok) { 14 return res.json(); 15 } else { 16 throw new Error(res.statusText); 17 } 18} 19 20// React-queryを用いた天気情報の取得 21export default function QueryBasic() { 22 const {data, isLoading, isError, error} = useQuery("weather", fetchWeather); 23 24 // 非同期処理が途中の場合(ビュー) 25 if(isLoading) { 26 return ( 27 <p>Now Loading...</p> 28 ) 29 } 30 31 // 非同期処理がエラーの場合(ビュー) 32 if(isError) { 33 return ( 34 <p>Error: {error.message}</p> 35 ) 36 } 37 38 39 // 天気情報取得後のビュー 40 return ( 41 <figure> 42 <img src={`https://openweathermap.org/img/wn/${data?.weather?.[0]?.icon}.png`} /> 43 <figcaption>{data?.weather?.[0]?.description}</figcaption> 44 </figure> 45 ) 46}
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
https://zenn.dev/counterworks/articles/react-query-update-v4-to-v5
補足
特になし

回答1件
あなたの回答
tips
プレビュー