-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
[RFR] Add enabled options to query hooks #5849
[RFR] Add enabled options to query hooks #5849
Conversation
You should implement the enabled prop only once in useDataProvider. When enabled is false, it should return an empty resolved Promise (just like for optimistic queries). |
I moved the implementation to the One thing I noticed and that I'm not sure I'm happy with is that when {
data: {}
error: null
ids: []
loaded: false
loading: true
total: undefined
} I think that Similarly, if the query was enabled at some point and then disabled, the result is: {
data: {...Data}
error: null
ids: [...Ids]
loaded: true
loading: false
total: 20
} Here, I'm wondering if Regarding the tests, where would you write them? Inside |
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.
For the result in case of disabled query, I'm not sure this is really important. And I have no idea of what a disabled query should return. Feel free to suggest what you think is best.
As for unit tests, please add tests for the enabled options the existing useDataProvider and useMutation tests.
@@ -57,7 +67,7 @@ const useGetList = <RecordType extends Record = Record>( | |||
pagination: PaginationPayload, | |||
sort: SortPayload, | |||
filter: object, | |||
options?: any | |||
options?: UseGetListOptions |
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.
use the existing UseDataProviderOptions
instead
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.
Good point, I did it as well for useGetOne()
.
@@ -143,6 +144,7 @@ export interface Query { | |||
|
|||
export interface QueryOptions { | |||
action?: string; | |||
enabled?: boolean; |
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.
You should add options.enabled to the dependency list of the useEffect, as you did in usequeryWithStore
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.
Having options.enabled
in the useEffect there is actually a left-over that I forgot to remove when removing the early return in the hook. As the logic is now handled in useDataProvider
and this option is part of the requestSignature
, I don't think this is needed.
The only think that seems meaningful to me regarding the I'll work on the tests later today. |
@fzaninotto I pushed a first test and I noticed that because we return an empty resolved Promise when enabled is false, the data provider resolves with I don't think this is right. |
How do you suggest we fix this Type problem? I see that react-query types the data as optional in the response type. |
This is actually not only a type problem but can lead to runtime error: see this line in the test I created. The problem is not that In the case of getOne, I think we should resolve with |
@fzaninotto I'm not sure how to proceed with this PR, do you have any suggestions please? |
Caution: your PR needs rebase |
7d82d1c
to
de37d12
Compare
Excellent! Now you just have to document the new option and we're good to merge. |
Documentation added. |
Thanks! |
Closes #5827
description
This PR introduces the
enabled
option to theuseQuery()
,useQueryWithStore()
and all the corresponding specialised hooks. The goal is to be able to conditionally make a query. It can be useful for example for a query depending on another oneopen questions
enabled: false
? For now, it will contain the last value beforeenabled: false
, i.e. the initial date on load and then the last result ifenabled
switches fromfalse
totrue
.todos