Infinite recursion of custom hooks using useQueries #3428
Unanswered
ondrejvelisek
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I've encountered interesting problem with useQueries, custom react hooks, rule of hooks, and recursion.
I have a custom hook
useFilterRanges(filterIds)
. This hook uses useQueries and internaly calls (depends on) other custom hookusePrescendingQueryFilters(filterIds)
. This hook depends onuseQueryFilters(filterIds)
and it cyclickly depends onuseFilterRanges(shorterFilterIds)
. Maybe it seems like a logical defect, but it is just a recurion because shorterFilterIds are strictly shorter array. Without rule of hooks, I would wrote recursion break like:But it is prohibited by rule of hooks (and I understand why). Without the break JavaScript throws
Maximum call stack size exceeded
.How would you solve this? How to break the cycle? What is a React-Query idiomatic way? For now I have no idea how to solve it.
Thank you.
Code example:
Note the code is simplified (I've doublechecked but it can contain some typos) and the real project cycle is even bigger, but I believe this is kind of minimal meaningful example. :)
Beta Was this translation helpful? Give feedback.
All reactions