-
Hello, I upgraded from v3 to v4 today and was unpleasantly surprised to find that undefined is no longer an allowed return value for queryFn. It broke my application, and while debugging I remembered that I saw this information in the migration guide. In reference to #4910, I agree with the opinion that this is very prone to human error. It's a pity that typescript doesn't report anything wrong either. It is quite hard to find any information on this. Why it is so, etc. I've solved this thanks to the migration guide, but I lack information on this anywhere in the documentation if I'm not looking wrong, so I think newcomers may come across this often and it should be highlighted. For example, https://tanstack.com/query/v4/docs/react/guides/query-functions says "A query function can be literally any function that returns a promise. The promise that is returned should either resolve the data or throw an error." which is not entirely accurate. The function must not return undefined in the promise which is a pretty significant detail. I also wonder if undefined is forbidden for mutationFn as well, where empty-undefined response data is a common case. I solved this with axios intercepting every response and replacing undefined with null. Is this correct way? Please don't take this offensively, I'm just describing my experience today. I think v3 was more developer friendly in this manner. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
That's what the migration guide is for - it tells you what you need to change when upgrading...
please create a PR to the docs to clarify this.
The migration guide gives reasons for why it is necessary. If you return
so effectively, you couldn't use the functional updater in some cases. Now, you can:
as It's also more streamlined with
if
Not sure why you have queries that don't return anything in the first place 🤷
v3 was more inconsistent. Please accept that we have to consider more use-cases than the one you're currently having and have to balance all the facts. |
Beta Was this translation helpful? Give feedback.
-
Thanks for clarification. I will adapt.
Well, I used "api functions (axios/fetch)" directly as query functions. Undefined was returned when there was no record found for id eg. GET /someResource/${id} returned 404 Not Found with empty body which resulted in undefined returned from api/query fn. Also my mutation api functions were returning undefined, as there was no need for anything else than success status. I was also following "practice" of using only undefined of undefined/null options to represent emtpy values as discussed here sindresorhus/meta#7. However it unfortunately seems, null just can not be avoided in our code in the end.
Sure, migrations guide is great, I just wanted to point out that newcomers probably wouldn't read migration guide for such info since they don't have anything to migrate yet and they will probably encounter returning undefined sooner or later. Is undefined forbidden for mutationFn as well? Probably is. I'm asking just to know whether mutate docs should also be updated? |
Beta Was this translation helpful? Give feedback.
-
I had this piece of code below as my axios request:
and this as my query hook
and still got the undefined not allowed as return value under the latest version. After removing the .then and .catch and used a try catch instead it worked. It seems React-Query does no longer accept the returned data inside the .then chain? data must be explicitly returned or it wont work? Perhaps, I am doing something wrong? because this has always worked for me. |
Beta Was this translation helpful? Give feedback.
-
Then how to approach this case, where i will be getting a |
Beta Was this translation helpful? Give feedback.
That's what the migration guide is for - it tells you what you need to change when upgrading...
please create a PR to the docs to clarify this.
The migration guide gives reasons for why it is necessary. If you return
undefined
fromsetQueryData
, we opt out of the update now. This wasn't possible before, so you needed to:so effectively…