-
-
Notifications
You must be signed in to change notification settings - Fork 642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add equality fn to atomWithQuery #386
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 965db9b:
|
As we discussed in discord, I'm not a big fan of equalityFn. But, this sounds unavoidable.
As this means, react-query returns an different object with the same content. |
src/query/atomWithQuery.ts
Outdated
const data = get(dataAtom) | ||
if (!data || !equalityFn(data, action.data)) { | ||
set(dataAtom, action.data) | ||
} | ||
const pending = get(pendingAtom) | ||
if (!pending.fulfilled) { | ||
pending.resolve(action.data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably better to keep the pair of dataAtom and pendingAtom in sync.
const data = get(dataAtom) | |
if (!data || !equalityFn(data, action.data)) { | |
set(dataAtom, action.data) | |
} | |
const pending = get(pendingAtom) | |
if (!pending.fulfilled) { | |
pending.resolve(action.data) | |
const data = get(dataAtom) | |
if (!data || !equalityFn(data, action.data)) { | |
set(dataAtom, action.data) | |
const pending = get(pendingAtom) | |
if (!pending.fulfilled) { | |
pending.resolve(action.data) | |
} |
src/query/atomWithQuery.ts
Outdated
@@ -60,7 +61,10 @@ export function atomWithQuery< | |||
} | |||
action.initializer(getQueryClient(get, set)) | |||
} else if (action.type === 'data') { | |||
set(dataAtom, action.data) | |||
const data = get(dataAtom) | |||
if (!data || !equalityFn(data, action.data)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!data || !equalityFn(data, action.data)) { | |
if (data === null || !equalityFn(data, action.data)) { |
Let's make it strict here. (tbh, i'm not certain if null
in L37 is valid to mean uninitialized. Can TData
ever be null
?)
@Aslemammad thoughts? |
Yeah, I think it does. I know they have this feature: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Would you like to add a note about equalityFn in docs?
Merging this now. |
This takes the same
equalityFn
signature fromselectAtom
and adds it to theatomWithQuery
helper. I have found this very helpful in my apps to help control rerenders while the query revalidates. Currently, if the query is set to refetch on an interval, when it sets the data atom again it will cause any component that uses the data to rerender even if nothing in the data has changed.Happy to explore other options, but this works for me currently.