Mathis
Mathis

Reputation: 179

RTK Query: Get one element from list depending on useGet...Query hook argument

I fetch a list of articles in an external api using RTK Query like so (in createAPI) :

getArticles: builder.query<{ data: ArticleDTO[] }, null>({
   query: () => "/api/v1/articles",
}),

It works great but now I need to get one single article and I don't have an endpoints to get one article. I was thinking to use RTK Query transformResponse to return one element from the list but I can't access the given query argument so I don't know how do do it !

Don't hesitate to ask for precision !

Upvotes: 2

Views: 2030

Answers (1)

phry
phry

Reputation: 44236

I would rather do that when you use the hook

const result = useGetArticlesQuery(undefined, {
  selectFromResult: result => ({ ...result, data: result.data?.some.thing })
})

Of course you could also abstract that into a custom hook.

This way, you won't make the same request multiple times to get your value.

Upvotes: 4

Related Questions