How to prevent query data selector function from running #2583
-
Hello, we use react-query in a lot of places within our UI and recently moved some complex sorting logic into these select handlers. I'm noticing that one select handler in particular that deals with somewhat large amount of data gets called many many times, and is leading to OOM issues for us. Is there a way to programmatically prevent the select handlers from running? Or do they always run? I have tried setting Anything you can think of I should try to limit the number of times this query is running its select handler? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
So You can read more about that in my blog here: https://tkdodo.eu/blog/react-query-data-transformations#3-using-the-select-option I'd also happily accept a PR to the docs that somehow mentions this behaviour, on some page where it fits :) |
Beta Was this translation helpful? Give feedback.
-
Ah, that sounds solid! Let me give it a spin and if it works and I can contribute to the docs I'd be happy to. Will send an another update soon. |
Beta Was this translation helpful? Give feedback.
So
selectors
generally run on every render, because most of the time, they are very inexpensive. Technically, they always run if the functional reference changes or if the input (request data) changes. To improve this, you an memoize the selector function, either by extracting it to have it outside of the component, or withuseCallback
if it has dependencies.You can read more about that in my blog here: https://tkdodo.eu/blog/react-query-data-transformations#3-using-the-select-option
I'd also happily accept a PR to the docs that somehow mentions this behaviour, on some page where it fits :)