英文:
RTK Query invalidateTags only after mutation "success"
问题
Hello, I understand that you want a translation of the provided code. Here's the translated code:
你好,我正在使用RTK Query进行API交互,但我遇到了一些错误,当我触发变异端点时,无论是错误还是成功,都会执行使其失效。
我要寻找的是在变异成功时使标签失效,以下是我的代码:
获取端点
```javascript
getPosts: builder.query({
query: () => `/posts`,
providesTags: ['Posts'],
transformResponse: (response, meta, arg) => response.data,
transformErrorResponse: (response, meta, arg) => response.data?.message ?? response,
})
变异端点
mutatePost: builder.mutation({
query: (data, id) => ({
url: id ? `/posts/${id}` : `/posts`,
method: id ? 'PATCH' : 'POST',
body: data,
}),
transformResponse: (response, meta, arg) => response.data,
transformErrorResponse: (response, meta, arg) => response.data?.message ?? response,
invalidatesTags: ['Posts'],
})
我尝试了所有可用的结果,但都不符合我的期望。
Please let me know if you need any further assistance.
<details>
<summary>英文:</summary>
Hello am using RTK query for API interaction but am facing some error, where I trigger mutation endpoint invalidation always executed if its error or success.
what i am searching for is Invalidate Tags on mutation success here is my code
Get Endpoints
`getPosts: builder.query({
query: () => `/posts`,
providesTags: ['Posts'],
transformResponse: (response, meta, arg) => response.data,
transformErrorResponse: (response, meta, arg) => response.data?.message ?? response ,
})`
mutation endpoint
` mutatePost: builder.mutation({
query: (data,id) => ({
url: id?`/posts/${id}`: `/posts`,
method: id?'PATCH':'POST',
body: data,
}),
transformResponse: (response, meta, arg) => response.data,
transformErrorResponse: (response, meta, arg) => response.data?.message ?? response ,
invalidatesTags: ['Posts'],
})`
i tried all results available but not fit for my expection
</details>
# 答案1
**得分**: 11
你可以使用回调符号
```js
invalidatesTags: (result, error) => error ? [] : ['Posts'],
英文:
You can use the callback notation
invalidatesTags: (result, error) => error ? [] : ['Posts'],
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论