react-query
@umijs/max comes with the react-query request strategy (which is the same as @tanstack/react-query).
How to enable
If using @umijs/max, enable by configuring.
export default {reactQuery: {},}
If using umi, first install the @umijs/plugins
dependency, then enable through configuration.
$ pnpm i @umijs/plugins -D
export default {plugins: ['@umijs/plugins/dist/react-query'],reactQuery: {},}
Features
The plugin does a few things for you,
Enable react query's devtool in dev mode, which can be disabled with
reactQuery: { devtool: false }
, and the option is configured in app.ts throughexport const reactQuery = { devtool }
.Register a global QueryClient, which can be disabled with
reactQuery: { queryClient: false }
, and the option is configured in app.ts throughexport const reactQuery = { queryClient }
.Most of react-query's exports can be imported and used from
umi
or@umijs/max
.
Configuration
The following configurations can be done in reactQuery
.
devtool
: boolean, whether to enable the official react query devtool, defaulttrue
queryClient
: boolean, whether to register a global QueryClient and QueryClientProvider, defaulttrue
For example:
export default {reactQuery: {devtool: false,queryClient: false,},}
Note: The above two configurations need to be configured in app.ts
at runtime.
Runtime Configuration
Includes the following configurations.
devtool
: objectqueryClient
: object
For example:
const API_SERVER = '/path/to/api/server';export const reactQuery = {devtool: {initialIsOpen: true,},queryClient: {defaultOptions: {queries: {queryFn: async ({ queryKey }) => {const res = await fetch(`${API_SERVER}/${queryKey.join('/')}`);if (res.status !== 200) {throw new Error(res.statusText);}return res.json();}}}},};