-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
RTK-Q: Types for custom createApi with custom conditional hooks #1072
Comments
Couldn't you just do TDefinitions[Keys] extends { extraOptions: { paginated: true } } ? Other than that: if you provide a playground or codesandbox that can showcase all of that, I can try to play around a bit with it, but generally we have all hands full to do preparing the release right now, so I cannot promise too much, sorry. |
I realised what I was trying to do is technically impossible as the interface declaration merging going on with So I'll rephrase my question instead: How do you make more advanced paginated queries with your library when you have the following requirements:
|
I honestly wouldn't overthink it too much. Stuff that has not been scrolled back to in a while will be collected from the cache, stuff that already is in the cache will be retrieved almost instantly. |
Doing it in the component is definitely also an option, however, I would have to repeat this pattern over and over for every component that uses a paginated query. This in turn bloats the components, which is why I want to encapsulate the behaviour. |
Doing it in component means "write it once, build a custom hook around it". Could look like And if you want to share that hook in the end - all the better. We still need documentation on infinite scrolling. No solution from one person will be applicable to another, but some (potenially simplified) examples as starting points for different scenarios would be great in there. |
I've thought about doing it like that, but then the problem becomes the types for usePagination(api.endpoints.X, otherArgs)
export function usePagination<TArgs, TResponse>(endpoint: ???, args: TArgs) {...} I could also pass in the action creator from StartQueryActionCreator<QueryDefinition<GetPokemonsRequest, AxiosBaseQueryFn, never, GetPokemonsResponse, string, Record<string, any>>> |
The "what types we expose" is certainly one of the last things we need to go through when releasing. For now it should be sufficient to import the types from |
Created a custom hook and it works great. I'm importing the types from |
Thank you so much @mtodberg, I am building a custom useAutoSaveMutationHook autogenerated within createApi and this samples you provided saved my day. I had the implementation working already, though I was really struggling a lot getting the types correct, so that the auto-suggestion would work. Without your samples I would have never managed to get this working hah although still having some ts errors not yet resolved but at least auto-suggesting the endpoint now does it's job. |
I am trying to create a custom
createApi
as per the documentation example on the website, https://deploy-preview-1016--redux-starter-kit-docs.netlify.app/usage/rtk-query/customizing-create-api. Besides the regular modules,coreModule
andreactHooksModule
, my custom api will be adding a module for creating paginated hooks. I need to do this as the built-in pagination example on the website is too simple for my use-case. My paginated hook needs to support infinite scrolling, next/prev methods and more. Also what the REST API will be returning is quite different as it may contain lots of query parameters and not just the simple page number.Basically I want to be able to define a paginated endpoint like this:
This will then be the
createApi
definition:The
reactPaginatedHooksModule
will be defined as:I am then mimicking what is being done in the source-code of your
reactHooksModule
:And now the problem:
The problem is that I cannot get the typings correct so that
usePaginated<endpoint>Query
as a type on the api will be available whenextraOptions: { paginated: true }
is being passed. I think the problem is because theextraOptions
isunknown
and therefore gets lost in type inference. I need a way to type the extra options.The text was updated successfully, but these errors were encountered: