English 中文(简体)
如何阻止Recuux RTK 查询错误的重新计算
原标题:How to stop Redux RTK query from retrying on error

我有一些要求可能回去404个。 当他们这样做时,RTK会发出试射,导致数以百计的失败要求。 为什么它试图对错误和我能做些什么进行指责?

问题回答

如果你最后点错,RTK Query s use Query将在两种情况下发出请求:

  • you change the argument (that would always result in a new request)
  • you mount a component using this useQuery.

因此,如果看不见你的法典,我就认为,你的构成部分会重新出现,从而在不断发展之后又提出另一个要求。

你们需要定制你们的创造 复印机功能。 您可以停止永久地重新尝试,确定unstable__sideEffectsInRender 财产为假造

import {
  buildCreateApi,
  coreModule,
  reactHooksModule,
} from  @reduxjs/toolkit/dist/query/react ;

const createApi = buildCreateApi(
  coreModule(),
  reactHooksModule({ unstable__sideEffectsInRender: false })
);

export default createApi;

const{isError} = 使用质量;

if (isError) show error message else render the components.

在部件提供之前,通过使用Query检查遗嘱归还财产。 如果APICOR公司失败,那么Error就成为事实,那么部件就会落空,从而防止针对失败的情景提出多重的APIC申请。

2024年4月的答复: 您可以按行业条件调整:

import { Mutex } from  async-mutex 
import {
  BaseQueryApi,
  BaseQueryFn,
  FetchArgs,
  fetchBaseQuery,
  FetchBaseQueryError,
  retry,
} from  @reduxjs/toolkit/query 
import { RootState } from  @common/store 
import {
  BaseQueryArg,
  BaseQueryExtraOptions,
} from  @reduxjs/toolkit/dist/query/baseQueryTypes 
import { RetryOptions } from  @reduxjs/toolkit/dist/query/retry 

// HERE
const customRetryCondition = (
  error: FetchBaseQueryError,
  args: BaseQueryArg<BaseQueryFn>,
  {
    attempt,
    extraOptions: { maxRetries } = {},
  }: {
    attempt: number
    baseQueryApi: BaseQueryApi
    extraOptions: BaseQueryExtraOptions<BaseQueryFn> & RetryOptions
  },
) => {
  // retry on default condition
  const defaultCondition = attempt <= (maxRetries || 0)
  if (defaultCondition) {
    return true
  }

  // retry on network s errors
  if (error.status ===  FETCH_ERROR  || error.status ===  TIMEOUT_ERROR ) {
    return true
  }

  // other case (application logic error), don t retry
  return false
}

const baseQuery = retry(
  fetchBaseQuery({
    prepareHeaders: (headers, { getState }) => {
      const {
        userAuth: {
          token: { accessToken },
        },
      } = getState() as RootState
      const token = accessToken
      if (token) {
        headers.set( authorization , `Bearer ${token}`)
      }

      return headers
    },
    baseUrl,
  }),
  {
    maxRetries: 5,
    // HERE
    retryCondition: customRetryCondition as unknown as undefined, // avoiding typescript error
  },
)




相关问题
How to use one react app into another react app?

I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...

how to get selected value in Ant Design

I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...

How to add a <br> tag in reactjs between two strings?

I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...

热门标签