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

useGetMany loading/loaded state does not change when query updated #6420

Closed
jeremydrichardson opened this issue Jul 7, 2021 · 2 comments · Fixed by #6913
Closed

useGetMany loading/loaded state does not change when query updated #6420

jeremydrichardson opened this issue Jul 7, 2021 · 2 comments · Fixed by #6913
Labels

Comments

@jeremydrichardson
Copy link

What you were expecting:
Using useGetMany, whenever the query parameter changes (the list of ids), the loading/loaded state should change.

What happened instead:
The loading/loaded state of useGetMany is only true/false on the first run of the query. If you update the list of ids you want to get, loading/loaded remains false/true even though you are getting new data. This means any checks for loaded state will return true even though the data isn't actually loaded yet.

Steps to reproduce:

  • Use a useGetMany hook to call a resource and pass in a stateful variable as the list of ids.
  • update the stateful variable to trigger a refresh of the data
  • check the state of loading/loaded

Related code:
https://codesandbox.io/s/react-admin-usegetmany-issue-tucjj

Go to the url /custom2 in the CodeSandbox browser.

All code is in the customRouteLayout.tsx file

Sample of the primary code at issue

const [postIds, setPostIds] = React.useState([1, 2]);

const { data, loaded } = useGetMany("posts", postIds, {
    enabled: postIds.length > 0
});
console.log("loaded", loaded);
console.log("data", data);

const updatePostIds = () => {
  setPostIds([1, 2, 3, 4, 5, 6]);
};

Other information:
I think the loading/loaded state might just be getting set too late. It appears as though the redux store is populated with the a number of undefined objects based on the number of ids passed to useGetMany. Perhaps the loading/loaded state are getting set after this? Problem is that then your component sees a bunch of undefined items it tries to process and inevitably leads to errors because it can't access any properties on those objects because they haven't actually been loaded yet.

Can the loading/loaded state be set before the undefined elements are added to the redux store?

Environment

  • React-admin version: 13.16.4
  • React version: 17.0.2
  • Browser: Chrome
@djhi djhi added the bug label Jul 21, 2021
@djhi
Copy link
Contributor

djhi commented Jul 21, 2021

That's a bug indeed. Thanks for the report!

@chinmaydalvi
Copy link

@djhi Can I work on this issue?

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