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

标签 reactjs react-redux react-hooks redux-toolkit rtk-query

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

在按下登录按钮对其进行测试后,我看到网络请求顺利通过(状态代码 200),并且我得到一个提供 usertoken 的响应对象,但是,当我尝试使用 useLoginMutation 获取返回数据时,我得到了一个未定义的值。

下面是注入(inject)到基本 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

然后在我的 React 组件中,我解构了变异结果,如下所示:

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

const submitLogin = () => {
  // pass in username password from the form
  login({username, password});
}

假设如果我控制台注销 dataisLoading 我假设我会看到 data: {user: "abc", token: "xyz"} ,因为在检查窗口的网络选项卡下我可以看到此网络请求的响应,但我看到的是 data: undefined

有没有人有解决这个问题的经验?

最佳答案

哦,我找到原因了,是一个非常粗心的错误。我不得不将 reducer 包裹到我的商店,这正是我所缺少的

关于reactjs - Redux 工具包 RTK 查询突变未获取返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68960754/

相关文章:

javascript - 类型错误 : Cannot read property 'setState' of undefined using event

node.js - 表达 typescript react : Send a json response after sending the view from the same request

node.js - 仍使用IE显示页面上不存在的旧数据

reactjs - 如何在 React/Redux 应用程序的任何(重新)渲染后触发事件

javascript - "useRefs"变量是 div 引用的初始值?

reactjs - 有没有正确的方法在 useEffect 中使用对象作为依赖项?

reactjs - react 钩子(Hook) - 调度操作后未调用useEffect

javascript - 从 JS 文件调用组件函数

javascript - 使用 create-react-app 构建后的空白页面

javascript - 在redux中,使用combineReducers预加载状态