React-query admin wrapper and possibily undefined value with useGetOne
See original GitHub issueAccording 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
However, the typing of the function requires the id
to be a valid identifier:
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:
- Created a year ago
- Comments:6 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 theid
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.
Indeed, this issue remains open, so any PR addressing it is welcome!