Is it possible to define RTK Query queries without QueryArgs?
Asked Answered
F

1

6

The definition for a query in RTK Query is query<ResultType, QueryArg>. All of this is fine for queries where I actually need to send parameters to the API. However, I have some endpoints which don't require anything to be sent (something like getAllX). What can I pass as QueryArg here in the template? It won't accept having one argument.

So far I've used types like undefined and null to denote that nothing needs to be sent, but it looks ugly when you use the hook: const {...} = useGetXQuery(undefined), and I'm pretty sure there has to be a better way, but scouring the internet yielded no results.

Francisfrancisca answered 10/5, 2023 at 7:51 Comment(2)
Have you tried passing empty object like {}? It's way cleanerHackneyed
@Hackneyed yup, I was just hoping to have no parameters in there. I found the solution though, posted it as an answer. I totally forgot about the void type.Francisfrancisca
F
16

Figured it out, you need to use void as a type, then you can use the hook like this: const {...} = useGetXQuery().

Francisfrancisca answered 10/5, 2023 at 7:55 Comment(3)
FWIW this is mentioned in our docs, at redux-toolkit.js.org/rtk-query/… , although it's sorta buried: "If query doesn't have a parameter, then void type has to be provided explicitly."Ixion
Ah, thanks @markerikson, I completely missed it.Francisfrancisca
I missed this too. I think this should be more conspicuous some examples online use undefined but it's ugly this one is more correct. cc: @IxionRounding

© 2022 - 2024 — McMap. All rights reserved.