No QueryClient set, use QueryClientProvider error occurred even though QueryClient set and use QueryClientProvider were specified.
未设置QueryClient,即使指定了QueryClient Set和Use QueryClientProvider,也出现了Use QueryClientProvider错误。
After a certain period of time in the above state, sometimes normal components are displayed again, and sometimes a blank screen appears.
在上述状态下,经过一段时间后,有时会再次显示正常组件,有时会出现空白屏幕。
import { useRoutes } from "react-router-dom";
import { routes } from "./routes";
import { QueryClientProvider } from "@tanstack/react-query";
import { getClient } from "./queryClient";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
const App = () => {
const elem = useRoutes(routes);
const queryClient = getClient();
return (
<QueryClientProvider client={queryClient}>
{elem}
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
);
};
export default App;
import { QueryClient } from "@tanstack/react-query";
type AnyOBJ = { [key: string]: unknown };
const BASE_URL = "https://fakestoreapi.com";
export const getClient = (() => {
let client: QueryClient | null = null;
return () => {
if (!client) client = new QueryClient({});
return client;
};
})();
export const fetcher = async ({
method,
path,
body,
params,
}: {
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
path: string;
body?: AnyOBJ;
params?: AnyOBJ;
}) => {
const url = `${BASE_URL}${path}`;
const fetchOptions: RequestInit = {
method,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": BASE_URL,
},
};
try {
const res = await fetch(url, fetchOptions);
const json = await res.json();
return json;
} catch (err) {
console.error(err);
}
};
export const QueryKeys = {
PRODUCTS: "PRODUCTS",
};
version
"@tanstack/react-query": "^4.35.0",
"@tanstack/react-query-devtools": "^4.35.0",
"react": "^18.2.0",
"react-query": "^3.39.3",
版本“@tanSTACK/REACT-QUERY”:“^4.35.0”,“@TANSTACK/REACT-QUERY-DevTools”:“^4.35.0”,“REACT”:“^18.2.0”,“REACT-QUERY”:“^3.39.3”,
更多回答
I tried running your code online with exact same dependency versions, and it is working just fine. Your dependency tree might be incorrectly resolved. Deleting and reinstalling node_modules
might fix the issue. And, I also believe you no longer need v3 react-query
since you're already using v4 @tanstack/react-query
.
我试着用完全相同的依赖项版本在线运行您的代码,它运行得很好。您的依赖关系树可能未正确解析。删除并重新安装NODE_MODULES可能会解决该问题。而且,我也相信您不再需要v3的REACT-QUERY,因为您已经在使用v4@TANSTACK/REACT-QUERY了。
我是一名优秀的程序员,十分优秀!