Skip to content
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

When useGetList is in the disabled state, when the cache is updated somewhere, the error value is undefined. #8940

Closed
zhiguow opened this issue May 24, 2023 · 3 comments · Fixed by #8941
Labels

Comments

@zhiguow
Copy link

zhiguow commented May 24, 2023

What you were expecting:
In the same resources, I have a function of GetList when the condition is true (that is, when the condition is not true, GetList is in a disabled state, which is not true by default); at this time, I click delete. I expect the deletion to be successful and no error is reported.

What happened instead:
Under the same resources, when I execute useDelete successfully, useGetList whose status is disabled reports an error.

Steps to reproduce:
Click the delete button and delete successfully.

Related code:

const Example = () => {
    const [deleteOne] = useDelete();
    useGetList(Resources.Temp, {}, { enabled: false });
    return <Button onClick={() => deleteOne(Resources.Temp, { id: '001' })}>{'delete one'}</Button>;
};

Analysis:
In my useGetList, I set the value of enabled as a condition. When the condition is triggered, the list will be obtained. At this time, the value of my useGetList is undefined; when I execute useDelete successfully, it tries to update the cache. Execute the onSuccess passed to the third parameter of useQuery in the source code of useGetList, and onSuccess tries to get the data from the value, resulting in an error.

Solution
1 In all useGetLists that are disabled by default, add ‘ initialData: { data: [] } ’

useGetList(Resources.Temp, {}, { enabled: false, initialData: { data: [] } });

2 Do not use useDelete, use dataProvider.delete, and use the refresh method in finally

    const dataProvider = useDataProvider();
    const refresh = useRefresh();
    const handleDelete = () =>  dataProvider.delete(Resources.Temp, { id: ‘001’ }) .finally(() => refresh());

Environment

  • React-admin version:4.9.3
  • React version:18.2.0
@zhiguow zhiguow changed the title When useGetList is in the disabled state, when the cache is updated somewhere, the error value is undefined. [BUG] When useGetList is in the disabled state, when the cache is updated somewhere, the error value is undefined. May 24, 2023
@zhiguow zhiguow changed the title [BUG] When useGetList is in the disabled state, when the cache is updated somewhere, the error value is undefined. When useGetList is in the disabled state, when the cache is updated somewhere, the error value is undefined. May 24, 2023
@slax57 slax57 added the bug label May 24, 2023
@slax57
Copy link
Contributor

slax57 commented May 24, 2023

Thanks for the report.

I would have appreciated a reproduction sandbox, but your report was detailed enough so that I could understand the problem anyway.

Both your workarounds are valid, although, I'd recommend the first one (providing initialData) because the second one is basically bypassing react-query altogether and hence removing its benefits.

In any case, I believe that useGetList.ts should be fixed because this proves we should not assume that value has a data property at anytime.
Hence I'll label this as a bug.

@ArthurWD
Copy link

This issue is also present on useGetManyReference. I rewrote it to use useGetList to bypass it, but I think useGetManyReference should be fixed as well.

@slax57
Copy link
Contributor

slax57 commented Jul 20, 2023

@ArthurWD nice catch! Fixed by #9116

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants