RTK-Query updateQueryData callback is not called
See original GitHub issueI am trying to manually add query data cache for each item in list, after loading whole list. It’s simple table view, after which I would like to have instant load on each details view for any of the items in the list… And I can’t make it work, seems like updateQueryData callback is not called.
Keep in mind that at the moment when getAll function gets called, there isn’t any getById queryes in cache! I am not sure if that’s issue here…
This is my api code for getAll(list view):
getAll: builder.query({
query: () => `getAll`,
providesTags: (result) =>
result
?
[
...result.map(({ id }) => ({ type: "Fruits", id })),
{ type: "Fruits", id: "LIST" },
]
: [{ type: "Fruits", id: "LIST" }],
async onQueryStarted(_, { dispatch, queryFulfilled }) {
try {
const { data } = await queryFulfilled;
//here I am getting my list...
data.map((fruit) => {
dispatch(
fruitApi.util.updateQueryData("getById", fruit.id, (draft) => {
console.log("hi mom");
return Object.assign(draft, fruit);
})
);
});
} catch (e) {
console.log(e);
}
},
}),
And this is my getById(details view) api code:
getById: builder.query({
query: (id) => `getById?id=${id}`,
providesTags: (result) =>
result
? [{ type: "Fruits", id: result.id }] : [],
}),
Issue Analytics
- State:
- Created a year ago
- Comments:13 (2 by maintainers)
Top Results From Across the Web
RTK Query's updateQueryData gives an incorrect draft
The problem is that when I'm trying to update the cache, using updateQueryData, after making an API call to update one of the...
Read more >API Slices: Utilities
If no existing cache entry is found, the updateRecipe callback will not run. Example 1. const patchCollection = dispatch(
Read more >Manual Cache Updates
RTK Query > Usage > Manual Cache Updates: Updating cached data ... will not be called, and no patches or inversePatches will be...
Read more >Redux Essentials, Part 8: RTK Query Advanced Patterns
It takes three arguments: the name of the endpoint to update, the same cache key value used to identify the specific cached data,...
Read more >Usage With TypeScript
We strongly recommend using TypeScript 4.1+ with RTK Query for best results. If you encounter any problems with the types that are not...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Okay I got it, there was a problem with the type in this case after converting id to string in line before it started working… I guess adding this at least to documentation (correct me if I am wrong but I haven’t found anything about this), or loging some warning/error would be nice… 😄 But I’m glad I got it to work… Thanks for help!
updateQueryData
only updates existing data for existing cache entries. It is not meant to add new cache entries.