如何在rtkquery中构成错误,以及如何将它们结合在自定义钩中

发布于 2025-01-22 08:25:00 字数 838 浏览 2 评论 0原文

如果我有一个React视图,该视图需要通过RTKQuery Cache/API调用进行两个查询,则如何将isloadingerror等组合组合?

操作

我已经想过要做以下

const useUsersAndHousesQueries = () => {
    const users = useUsersQuery()
    const houses = useHousesQuery()

    const isLoading: boolean = users.isLoading || houses.isLoading
    const isError: boolean = users.isError || houses.isError
    // ...users || ...houses boolean values
    
    const data = {users: users.data, houses: houses.data}
    const error = {...} // How are RTKQuery's errors structured? Or do I structure them myself? Is it derived from the api call's response? I want to combine them so they can be rendered in an alert together in a component as a warning

    return {isLoading, isError, data, error, ...}
}

If I have a react view that requires two queries to be made from an RTKQuery cache/api calls, how can I combine the isLoading, error, etc?

I've thought of doing the following but I'm not sure how the error objects are composed

Could possibly write a special hook that does just this

const useUsersAndHousesQueries = () => {
    const users = useUsersQuery()
    const houses = useHousesQuery()

    const isLoading: boolean = users.isLoading || houses.isLoading
    const isError: boolean = users.isError || houses.isError
    // ...users || ...houses boolean values
    
    const data = {users: users.data, houses: houses.data}
    const error = {...} // How are RTKQuery's errors structured? Or do I structure them myself? Is it derived from the api call's response? I want to combine them so they can be rendered in an alert together in a component as a warning

    return {isLoading, isError, data, error, ...}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱皱眉﹌ 2025-01-29 08:25:00

如果您使用fetchbasequery,则是fetchBaseQueryError或(在非常意外的情况下

) ,<代码>数据将是服务器的响应(如果以某种形式使用)

export type FetchBaseQueryError =
  | {
      /**
       * * `number`:
       *   HTTP status code
       */
      status: number
      data: unknown
    }
  | {
      /**
       * * `"FETCH_ERROR"`:
       *   An error that occurred during execution of `fetch` or the `fetchFn` callback option
       **/
      status: 'FETCH_ERROR'
      data?: undefined
      error: string
    }
  | {
      /**
       * * `"PARSING_ERROR"`:
       *   An error happened during parsing.
       *   Most likely a non-JSON-response was returned with the default `responseHandler` "JSON",
       *   or an error occurred while executing a custom `responseHandler`.
       **/
      status: 'PARSING_ERROR'
      originalStatus: number
      data: string
      error: string
    }
  | {
      /**
       * * `"CUSTOM_ERROR"`:
       *   A custom error type that you can return from your `queryFn` where another error might not make sense.
       **/
      status: 'CUSTOM_ERROR'
      data?: unknown
      error: string
    }

export interface SerializedError {
  name?: string
  message?: string
  stack?: string
  code?: string
}

If you are using fetchBaseQuery, it is either FetchBaseQueryError or (in very unexpected cases) a SerializedError:

In the case of a FetchBaseQueryError, data will be the response from the server (if available in some form)

export type FetchBaseQueryError =
  | {
      /**
       * * `number`:
       *   HTTP status code
       */
      status: number
      data: unknown
    }
  | {
      /**
       * * `"FETCH_ERROR"`:
       *   An error that occurred during execution of `fetch` or the `fetchFn` callback option
       **/
      status: 'FETCH_ERROR'
      data?: undefined
      error: string
    }
  | {
      /**
       * * `"PARSING_ERROR"`:
       *   An error happened during parsing.
       *   Most likely a non-JSON-response was returned with the default `responseHandler` "JSON",
       *   or an error occurred while executing a custom `responseHandler`.
       **/
      status: 'PARSING_ERROR'
      originalStatus: number
      data: string
      error: string
    }
  | {
      /**
       * * `"CUSTOM_ERROR"`:
       *   A custom error type that you can return from your `queryFn` where another error might not make sense.
       **/
      status: 'CUSTOM_ERROR'
      data?: unknown
      error: string
    }

export interface SerializedError {
  name?: string
  message?: string
  stack?: string
  code?: string
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文