Get rid of the option "enabled" #4765
-
The main idea here is useQuery is a hook, one of the hook's rule is "All the hooks should be executed unconditionally". But useQuery is quite special here) The second idea is — difficulties of a query state management during render. I mean, that we have to handle not only error/loading/success, but paused too. Cause of that flag we have to develop some ways, how to use it with TypeScript https://tkdodo.eu/blog/react-query-and-type-script#type-safety-with-the-enabled-option. But as for me (sorry @TkDodo) it's just a spike, not a solution. And sometimes "enabled" is like a fast patch in all of the cases in my opinion. Do not think, just use enabled. I know, that it is a problem of developers in the first place, but, as I think, TanStack query should learn how to write good code =) When do we need that option?There is no params for a queryActually you have such example in the docs: https://tanstack.com/query/v4/docs/react/guides/disabling-queries#lazy-queries
So, we need to render the child component with useQuery only if we have any truthy value in Conditionals queriesconst q1 = useQuery(['q1']);
const q2 = useQuery(['q2', q1.data]); Actually this problem has the same solutions — conditional rendering =) I'm glad to see another examples, when we need such option. And I wanna discuss it here. So, right now I have 2 questions and 1 idea. Questions:
And the idea — to get rid of it =) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
Beta Was this translation helpful? Give feedback.
-
Because it's not possible to call hooks conditionally. It's also not always easily possible or desired to split things up into multiple components, especially when writing custom hooks. Consider:
sure you can split that up into multiple components, but that would mean prop-drilling something that you read from a global client state manager (like redux in this example) and you can't really use custom hooks composition nicely. it also occurs frequently when reading |
Beta Was this translation helpful? Give feedback.
Because it's not possible to call hooks conditionally. It's also not always easily possible or desired to split things up into multiple components, especially when writing custom hooks. Consider:
sure you can split that up into multiple components, but that would mean prop-drilling something that you read from a global client state manager (like redux in this example) and you can't really use custom hooks composition nicely.
it also occurs fr…