-
-
Couldn't load subscription status.
- Fork 1.2k
Description
Problem
I've noticed that the selectFromResult callback in useQuery seems to be running any time any data in the store changes.
I'm trying to understand if this behavior is necessary.
Expected Behavior
I'd prefer selectFromResult to only run when the relevant data changes (on changes to loading state or the response data).
Benefit
The primary benefit of improving this behavior is that if someone (intentionally or accidentally) returns an unstable response from selectFromResult, the component will only re-render twice for every request that gets made (isLoading is set to true then false).
Reproduction
I have a simple sandbox that illustrates this behavior here.
I intentionally return an unstable reference in selectFromResult for useQuery B to show how many times selectFromResult is running. (The code sandbox was not reliably emitting console logs.)
How to fix
The implementation of selectFromResult is here.
I wasn't clever enough to figure out a way to fix it myself, but my hope is that the selectFromResult selector can be modified to only run when the relevant store data changes.
Note
I noticed there are a couple of existing issues that may be relevant to the discussion:
- selectFromResult running too often? #2208
- [RTK Query]: Problems with rerenders with useMemo createSelector #4002
I created a separate issue because I'm specifically looking to understand why this behavior is happening and whether it's possible to improve it.