Redux 工具包 RTK 查询突变未获取返回数据

use*_*580 7 reactjs react-redux react-hooks redux-toolkit rtk-query

createApi您好,我最近学习了带有 rtk 查询工具的新 React 工具包,并且我正在尝试使用rtk 包中的工具将登录系统放在一起。

After giving it a test on the login button pressed, I see the network request going through without any issue(status code 200), and I get a response object providing user, token, however, when I try to get the returning data using useLoginMutation I get an undefined value.

below is the code for my endpoint which is injected in a base api:

export const apiLogin = theiaBaseApi.injectEndpoints({
  endpoints: (build) => ({
    loginUser: build.mutation<UserReadonly, loginValuesType | string>({
      query: (values: loginValuesType, redirect?: string) => {
        const { username, password } = values;
        const header = gettingSomeHeaderHere

        return {
          url: "login",
          method: "GET",
          headers,
          crossDomain: true,
          responseType: "json",
        };
      },
    }),
  }),
});

export const { useLoginUserMutation } = apiLogin
Run Code Online (Sandbox Code Playgroud)

then inside my React component I destructure the mutation result such like below:

const [login, {data, isLoading}] = useLoginUserMutation();

const submitLogin = () => {
  // pass in username password from the form
  login({username, password});
}
Run Code Online (Sandbox Code Playgroud)

Suppose if I console log out data and isLoading I assume that I will see data: {user: "abc", token: "xyz"}, because under network tab of my inspect window I can see the response of this network request, but instead I am seeing data: undefined

Does any have experience on solving this?

use*_*580 7

哦,我找到原因了,这是一个非常粗心的错误。我必须将减速器包装到我的商店,这正是我所缺少的