-
I'm using the code generation feature of RTK query and it has been great. I now want to add an onQueryStarted function to do something either before an API call executes or after it completes. I've got it working just fine but only by manually adding my onQueryStarted code to the generated .ts file. Obviously this isn't the correct thing to do because when I regenerate the code, my changes will be lost... is there any supported way or best practice that allows me to write my onQueryStarted code and have the API regenerated without overwriting my changes? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Yes. Use enhanceEndpoints in another file and import the hook from that new file. |
Beta Was this translation helpful? Give feedback.
-
Awesome, thanks so much! This works great. One little snag I had (due to my inexperience) was that I'm exporting the enhanced API from the new file as the default export but the generated file doesn't export the the API as its default so I had errors in the way I was importing it into other files. I also somehow expected I'd be able to magically import the API types from the new file as well but those have to be imported from the generated file of course. Just thought I'd mention in case it might help other newcomers. I ended up naming the generated file "apiTypes" and my new file "api." It is a little misleading since the apiTypes file also contains the generated api but really, I only ever expect to import types from there and specifically don't want to import that api since I now only want to use the enhanced one. Perhaps there is a way to separate the generated types from the generated api so I could have an api.ts (for my enhanced api), an apiGenerated.ts (for the generated api) and an apiTypes.ts file just for the types? |
Beta Was this translation helpful? Give feedback.
Yes. Use enhanceEndpoints in another file and import the hook from that new file.