react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

List props queryOptions enabled set to false causes type error.

Open yanchesky opened this issue 3 years ago • 3 comments

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

Zrzut ekranu 2022-07-18 o 22 57 55

Steps to reproduce:

  1. Run the simple-demo.
  2. Provide queryOptions={{ enabled: false }} in any List component.
  3. 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): -

yanchesky avatar Jul 18 '22 21:07 yanchesky

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?

fzaninotto avatar Jul 20 '22 07:07 fzaninotto

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.

yanchesky avatar Jul 20 '22 07:07 yanchesky

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.

fzaninotto avatar Jul 20 '22 09:07 fzaninotto