readux工具包查询中的EnhanceEndPoints()在做什么?

发布于 2025-02-10 18:07:43 字数 1030 浏览 3 评论 0 原文

根据官方redux工具包官方查询文档(以获取EnhanceEndpoints) )

与注射点不同,部分端点定义不会 替换现有的定义,但在 每次定义基础(即,object.Assign) newPartialEndPoint))。

我们不能对以下内容执行此操作?

api.injectEndpoints({ overrideExisting: false, endpoints:...})

那么,为什么我们需要额外的API呢?

另外,我注意到那里给出的示例:

import { api } from './api'
const enhancedApi = api.enhanceEndpoints({
  addTagTypes: ['User'],
  endpoints: {
    getUserByUserId: {
      providesTags: ['User'],
    },
    patchUserByUserId: {
      invalidatesTags: ['User'],
    },
    // alternatively, define a function which is called with the endpoint definition as an argument
    getUsers(endpoint) {
      endpoint.providesTags = ['User']
      endpoint.keepUnusedDataFor = 120
    },
  },
})

...不使用

...(builder)=>...builder.query(...)...

这是某种替代语法吗?

According to the official Redux Toolkit Query documentation here for enhanceEndpoints():

Unlike injectEndpoints, the partial endpoint definitions will not
replace existing definitions, but are rather merged together on a
per-definition basis (ie, Object.assign(existingEndpoint,
newPartialEndpoint)).

Can't we already do this with the following?

api.injectEndpoints({ overrideExisting: false, endpoints:...})

So why do we need an additional API?

Also, I noticed the example given there:

import { api } from './api'
const enhancedApi = api.enhanceEndpoints({
  addTagTypes: ['User'],
  endpoints: {
    getUserByUserId: {
      providesTags: ['User'],
    },
    patchUserByUserId: {
      invalidatesTags: ['User'],
    },
    // alternatively, define a function which is called with the endpoint definition as an argument
    getUsers(endpoint) {
      endpoint.providesTags = ['User']
      endpoint.keepUnusedDataFor = 120
    },
  },
})

...does not use

...(builder)=>...builder.query(...)...

Is this some sort of an alternative syntax?

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

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

发布评论

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

评论(1

不甘平庸 2025-02-17 18:07:43

OverrideExisting: injectEndEndPoints 的false 表示,如果端点已经存在,则会收到一个错误消息,而端点也不会发生。如果 true ,它将替换您现有的端点。

=> InjectEndPoints 可以用全新的代码添加或替换端点,但是它无法修改端点定义。

EnhanceEndpoints 另一方面无法创建一个新的端点定义 - 但它允许您修改现有的定义。

It has two notations:

  • either you specify a partial endpoint definition as an object. That object will just be merged with the endpoint definition
  • or you have a callback function.这将使您访问旧的端点定义,也许可以阅读并写入它。

无论哪种方式:如果该端点以前不存在,则不会发生任何事情。

overrideExisting: false of injectEndpoints means that if the endpoint already exists, you get an error message and nothing happens to the endpoint. If true, it will replace your existing endpoint.

=> injectEndpoints can add or replace endpoints with completely new code, but it cannot modify an endpoint definition.

enhanceEndpoints on the other hand cannot create a new endpoint definition - but it allows you to modify an existing one.

It has two notations:

  • either you specify a partial endpoint definition as an object. That object will just be merged with the endpoint definition
  • or you have a callback function. That will allow you to access the old endpoint definition, maybe read it and also write to it.

Either way: if that endpoint did not exist before, nothing will happen.

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