[RTK Query] Support a transformRequest
function
#2495
huyenltnguyen
started this conversation in
General
Replies: 2 comments 7 replies
-
Your endpoint's |
Beta Was this translation helpful? Give feedback.
7 replies
-
Many times, I want to get the global state from the store as a query parameter, so I usually write some custom hooks to implicitly pass these global states. function useMyGetXXXQuery(rawQueryArg) {
const site = useSelector(selectCurrentSite)
const finalQueryArg = { site, ...queryArg }
return useGetXXXQuery(finalQueryArg)
} For this scenario, just my personal thoughts, I think it would be much better if there was a prepareQueryArg: // getXXX:
builder.query<Result, RawQueryArg>({
prepareQueryArg: (rawQueryArg, { getState }) => {
return {
site: selectCurrentSite(getState() as RootState),
...rawArg,
}
},
query: (finalArg) => ({ ... })
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi team 👋
I'm wondering if you have considered adding support for a
transformRequest
function to an endpoint, since we already havetransformResponse
supported.Right now I'm transforming the data in the UI component before passing it to a useQuery or useMutation hook, but pre-RTKQ, I was transforming API request and response outside of the UI layer. So I'm wondering if I can keep following this pattern with RTKQ, or if you have any recommendation / best practices for data transformation in general.
Beta Was this translation helpful? Give feedback.
All reactions