You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the examples below, useQuery is pseudo code to refer to an arbitrary query hook generated by @reduxjs/toolkit/query/react.
The current implementation performs a shallow equality check to allow the hook consumer to pass in inline objects without having to manually memoize these:
useQuery({foo: 123,bar: "baz"})// This is stable
However, if the consumer wants to use a nested object, the shallow equality will not be able to understand that no change has been made:
useQuery({nested: {foo: 123,bar: "baz"}});// This is not stable
To work around this the consumer would have to manually memoize the query argument, which easily becomes very verbose in larger projects with lots of queries and views, requiring an extra statement per query.
I suggest to provide the option to use any kind of equality checking:
useQuery({nested: {foo: 123,bar: "baz"}},{equalsFn: deepEquals});// This is stable
In the above example, useQuery would determine whether the argument has changed by calling the provided deepEquals function, instead of the default shallowEquals function.
Additionally, a central configuration could be provided, to modify the default equality checking function for all queries:
The text was updated successfully, but these errors were encountered:
kasper573
changed the title
Custom equality check for stableArg in useQuery from rtk-query/react
Custom equality check for determining argument changes in useQuery from rtk-query/react
Nov 25, 2021
This issue is referring to how
stableArg
is determined inbuildHooks.ts
/useQuerySubscription
:redux-toolkit/packages/toolkit/src/query/react/buildHooks.ts
Line 508 in 2ff1fa4
The problem
The current implementation performs a shallow equality check to allow the hook consumer to pass in inline objects without having to manually memoize these:
However, if the consumer wants to use a nested object, the shallow equality will not be able to understand that no change has been made:
To work around this the consumer would have to manually memoize the query argument, which easily becomes very verbose in larger projects with lots of queries and views, requiring an extra statement per query.
Suggested solution
I suggest to provide the option to use any kind of equality checking:
In the above example,
useQuery
would determine whether the argument has changed by calling the provideddeepEquals
function, instead of the defaultshallowEquals
function.Additionally, a central configuration could be provided, to modify the default equality checking function for all queries:
The text was updated successfully, but these errors were encountered: