question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React-query admin wrapper and possibily undefined value with useGetOne

See original GitHub issue

According to the React Query documentation, we have the possibility to block the running query thanks to the enabled function if one of our parameter is not defined. On the documentation sample, we can see userId can be set on the query definition without any issue.

We also have the possibility to use this option from the useGetOne hooks as you specified on the code doc block

https://github.com/marmelab/react-admin/blob/69d1cd1af77a432e2d1a3c2215ee93a2dedcd4be/packages/ra-core/src/dataProvider/useGetOne.ts#L26

However, the typing of the function requires the id to be a valid identifier:

https://github.com/marmelab/react-admin/blob/1ddb1672fe323baffb80567c890c759b3cea379c/packages/ra-core/src/types.ts#L139

Because of that, we have to cheat by casting the typing of the undefined value, for example:

const { data: userData } = useGetOne<UserRecord>('users', { id: userId || '' }, { enabled: !!userId });

According to me, we should make that param optional to match the react-query implementation.

What do you think?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
fzaninottocommented, Jun 16, 2022

Thanks for your proposal.

To me, what you suggest isn’t an improvement in the general case. If a user doesn’t use the enabled prop, they will see the id parameter as optional, which is misleading.

To make it work with TypeScript, the function should have two signatures: one with enabled enabled, where the params are optional, and one without. But I’m not even sure this is correct or feasible.

In other terms, I think the react-admin codebase shouldn’t handle this case. We should at most document the gotcha in the documentation of the enabled option.

That’s my opinion, I’m open to other points of view.

0reactions
fzaninottocommented, Sep 1, 2022

Indeed, this issue remains open, so any PR addressing it is welcome!

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-admin - Querying the API - Marmelab
This wrapper logs the user out if the dataProvider returns an error, and if the authProvider sees that error as an authentication error...
Read more >
Handling `undefined` in React Query and Typescript - neldeles
Generally, on the first render cycle, data will be undefined because well, the data is async and does not exist yet on the...
Read more >
how to avoid undefined using React Query (useQuery) for ...
To avoid undefined you need to check for loading and error. Here's a simple way to fetch list of users.
Read more >
Migrating to React Query 4 - TanStack
Undefined is an illegal cache value for successful queries. In order to make bailing out of updates possible by returning undefined , we...
Read more >
An opinioneted wrapper around react-query to implement fully ...
This library is a wrapper around react-query, so before you begin ... you most probably will have the value you need only when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found