-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat(queryClient): add setQueriesData utility #2204
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
Conversation
a function which can be used to fuzzily set queryData for multiple queries
|
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/tannerlinsley/react-query/EauHPTxmP8XGSQxuYLttSDo2mvm3 |
|
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 95bed80:
|
| filters: QueryFilters, | ||
| updater: Updater<TData | undefined, TData>, | ||
| options?: SetDataOptions | ||
| ): [QueryKey, TData][] |
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.
not sure if this is the best return type - wanted to provide a way to show which key actually had data set, as calling setQueriesData on a filter that doesn't match anything will not set anything
src/core/queryClient.ts
Outdated
| .findAll(queryKeyOrFilters) | ||
| .map(({ queryKey }) => [ | ||
| queryKey, | ||
| this.setQueryData<TData>(queryKey, updater, options), |
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.
@boschni I'm a bit worried that calling setQueryData multiple times in a row triggers multiple re-renders, and it does. see this example, where onButtonClick, I call setQueryData three times for three queries, and even though it's synchronous, it will trigger 3 re-renders:
https://codesandbox.io/s/tannerlinsleyreact-query-basic-forked-5c6rs?file=/src/index.js
I tried to wrap it in notifyManager.batchCalls, but it didn't change anything.
This is not much to do with this PR, but because I'm now calling setQueryData in a loop, it might show :)
Do you have any idea how to batch these calls maybe?
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.
couldn't find this out, but is also not so important I guess. if we get the api of that function right, we can always optimize it internally later :)
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.
Hi @TkDodo! The entire function body should be wrapped in notifyManager.batch, this should prevent redundant renders
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.
thank you 🙏 , seems like I messed up batchCalls and batch 😅 . fixed it here: 95bed80
chore: add prettier to pipeline checks and include .ts files (#2205)
improve docs
| .setData(updater, options) | ||
| } | ||
|
|
||
| setQueriesData<TData>( |
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.
since we are not creating queries like with setQueryData, because we only update matching keys, would updateQueries or updateQueriesData be a better name?
this was also mentioned by @tannerlinsley here: #135 (comment)
but I don't think it made it?
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.
I think the current name is fine as long as people realize that it will not create queries.
| setQueriesData<TData>( | ||
| filters: QueryFilters, |
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.
I didn't create an overload that accepts queryKey and filters, even though some other functions have this, because the updater is required so the signature would be very weird. I also don't think it's important because the filters also have the option to pass a queryKey.
wrap setQueriesData in notifyManager.batch to avoid unnecessary re-renders
|
🎉 This PR is included in version 3.16.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
still missing: