我想访问我的redux-toolkit存储在rtk-query
端点中的数据。
如何从query
或transformResponse
方法访问存储?
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
1条答案
按热度按时间wr98u20j1#
query
和transformResponse
都不能直接访问Redux状态,但是,您可以将两者替换为queryFn
。对于queryFn
,所使用的查询函数被传递给查询arg
、基本查询api
对象、任何已定义的extraOptions
和baseQuery
函数。使用api.getState
访问当前状态值。例如,您的代码可能类似于以下内容: