react-admin
react-admin copied to clipboard
List props queryOptions enabled set to false causes type error.
What you were expecting:
Providing queryOptions={{enabled: false}} in a List component should display nothing or empty component
What happened instead:
Error occurs because data returned from useQuery is undefined
Uncaught TypeError: Cannot read properties of undefined (reading 'length') at Datagrid.tsx:200:13

Steps to reproduce:
- Run the simple-demo.
- Provide
queryOptions={{ enabled: false }}in any List component. - Described error should be shown.
Related code:
👇 Fork of simple-v4 with queryOptions={{ enabled: false }} 👇
https://codesandbox.io/s/pedantic-glitter-dw39ic?file=/src/posts/PostList.tsx
Other information:
This undefined data returned from react-query's useQuery hook also affects useListController hook where page is set to 1 if there is no data. The part of code that does not expect data as undefined is:
useListController.ts
110 if (
111 ...
112 (!isFetching && query.page > 1 && data.length === 0)
113 ) {
114 ...
Environment
- React-admin version: 4.2.3
- Last version that did not exhibit the issue (if applicable): -
- React version: 17.0.0
- Browser: Chrome, Safari
- Stack trace (in case of a JS error): -
Thanks for the report! Setting enabled=false in the List view isn't something we imagined, so the fact that it's not supported doesn't surprise me. Would you mind explaining your use case?
The use case is:
Some queries require a parameter that may not be selected (yet). Until it's not selected I don't want the request to be run because it would result in an error. I set queryOptions={{ enabled: false }} to suspend that request. Once the parameter is selected, I change enabled to true, and then the query is run with provided parameter.
Thanks, this seems like a fair use case, so I think react-admin should support it. I'll mark this issue as an enhancement request.