cangsdarm / react-query-web-i18n Goto Github PK
View Code? Open in Web Editor NEWTanStack Query (react-query) doc i18n (zh-CN). Waiting to be polished
Home Page: https://cangsdarm.github.io/react-query-web-i18n/
License: MIT License
TanStack Query (react-query) doc i18n (zh-CN). Waiting to be polished
Home Page: https://cangsdarm.github.io/react-query-web-i18n/
License: MIT License
dependencies:
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-query": "^3.39.2",
code:
import axios from "axios";
import React from 'react';
import { createRoot } from 'react-dom/client';
import { QueryClient, QueryClientProvider, useQuery } from 'react-query';
const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false
}
}
})
function Layout() {
const { isLoading, isError, error, data } = useQuery(["fff", { aa: 111 }], (param) => {
// let tmp = param.signal;//访问了 signal就会执行2次 不访问就执行一次
return axios.get("http://www.httpbin.org/get");
})
if (isLoading) {
return <div>loading</div>
}
if (isError) {
return <div>{error.message}</div>
}
return <div>
<div>data:{data ? data.data ? data.data.origin : "" : ""}</div>
</div>
}
function Root() {
return <>
<QueryClientProvider client={queryClient}>
<Layout />
</QueryClientProvider>
</>
}
const container = document.getElementById('root');
const root = createRoot(container);
root.render(<React.StrictMode><Root /></React.StrictMode>);
Steps:
1. Invoke the login interface and use 'queryClient.setQueryData' to store the token.
2. Jump to the home page and invoke a new interface.
3. The local storage cannot be retrieved in axios' request interception.
Test:
I try not to use plugins and manually store them locally, when the login succeeds, the token is stored, and the new interface is invoked on the jump home page, the locally stored token can be obtained in axios request interception.
Code:
// login.tsx
const queryClient = useQueryClient()
const userData = useQuery({ queryKey: ['userData'], enabled: false })
const loginMutaion = useMutation(
(params: FormState) =>
loginService(...omit params),
{
onSuccess: async (res) => {
if (res.code === '200') {
queryClient.setQueryData(['userData'], res.data)
message.success('登录成功')
navigate('/common/Home')
}
}
}
)
login => loginMutaion.mutate
// App.tsx
import { QueryClient } from '@tanstack/react-query'
import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client'
import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister'
const queryClient = new QueryClient({
defaultOptions: {
queries: {
cacheTime: 1000 * 60 * 60 * 24 // 24 hours
}
}
})
const persister = createSyncStoragePersister({
storage: window.localStorage,
key: 'userData'
})
<PersistQueryClientProvider client={queryClient} persistOptions={{ persister }}>
<Suspense>
<Router />
</Suspense>
</PersistQueryClientProvider>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.