admin管理员组文章数量:1435859
I want to access to my redux-toolkit store data inside of the rtk-query
endpoints.
how can I access my store from query
or transformResponse
methods?
import { createApi } from '@reduxjs/toolkit/query/react'
import customFetchBase from './customFetchBase.js'
import { setUserInfo, setUserPermissions } from '../features/userSlice.js'
import { aesDEC } from 'src/util/public.util.js'
export const authApi = createApi({
reducerPath: 'authApi',
baseQuery: customFetchBase,
endpoints: builder => ({
getUser: builder.mutation({
query: () => ({
url: '/Account/Login/GetUserInfo',
method: 'POST',
body: {
RequestVerificationToken: salt //here I want the salt from my redux store
}
}),
transformResponse: response => {
return aesDEC(response.data, salt); //here I want the salt from my redux store
},
}),
})
export const { useGetUserMutation } = authApi
I want to access to my redux-toolkit store data inside of the rtk-query
endpoints.
how can I access my store from query
or transformResponse
methods?
import { createApi } from '@reduxjs/toolkit/query/react'
import customFetchBase from './customFetchBase.js'
import { setUserInfo, setUserPermissions } from '../features/userSlice.js'
import { aesDEC } from 'src/util/public.util.js'
export const authApi = createApi({
reducerPath: 'authApi',
baseQuery: customFetchBase,
endpoints: builder => ({
getUser: builder.mutation({
query: () => ({
url: '/Account/Login/GetUserInfo',
method: 'POST',
body: {
RequestVerificationToken: salt //here I want the salt from my redux store
}
}),
transformResponse: response => {
return aesDEC(response.data, salt); //here I want the salt from my redux store
},
}),
})
export const { useGetUserMutation } = authApi
Share
Improve this question
edited May 28, 2023 at 7:57
Alireza Kordkheili
asked May 28, 2023 at 5:50
Alireza KordkheiliAlireza Kordkheili
641 silver badge7 bronze badges
1
-
1
Replace
query
with aqueryFn
which will provideapi.getState
and also removetransformResponse
. WithqueryFn
you will do the work of both yourquery
and yourtransformResponse
– possum Commented May 28, 2023 at 9:35
2 Answers
Reset to default 5Neither query
nor transformResponse
have direct access to the Redux state, however, you can replace both with the queryFn
which does. With queryFn
the query function used is passed the query arg
, the base query api
object, any defined extraOptions
, and the baseQuery
function. Use api.getState
to access the current state value.
Example, your code might look similar to the following:
export const authApi = createApi({
reducerPath: 'authApi',
baseQuery: customFetchBase,
endpoints: builder => ({
getUser: builder.mutation({
queryFn: async (arg, api, extraOptions, baseQuery) => {
const state = api.getState();
const salt = state./* path to salt state value */;
try {
const { data } = await = baseQuery({
url: '/Account/Login/GetUserInfo',
method: 'POST',
body: {
RequestVerificationToken: salt
}
});
return { data: aesDEC(data, salt) };
} catch(error) {
return { error };
}
},
}),
})
});
You can also get access to the state in fetchBaseQuery/prepareHeaders function like this if you need to set Auth or some general header:
export const DataApi = createApi({
reducerPath: 'api',
baseQuery: fetchBaseQuery({
baseUrl: `${process.env.REACT_APP_API_URL}`,
prepareHeaders: (headers, {getState}) => {
const state = getState();
headers.set(HEADER_AUTH, state?.auth?.value?.authorization);
}
}),
...
});
本文标签: javascripthow can I access my store from RTKQuery endpointsStack Overflow
版权声明:本文标题:javascript - how can I access my store from RTK-Query endpoints? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745672360a2669648.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论