【问题标题】:Redux RTK Query: getDefaultMiddleware is not a functionRedux RTK 查询:getDefaultMiddleware 不是函数
【发布时间】:2021-11-04 19:06:54
【问题描述】:

getDefaultMiddleware 来自哪里?我正在阅读docs,它似乎神奇地出现在配置商店中。虽然这很好,但它并没有进入我的商店,而且......因为没有此功能的导入路径,我不知道如何解决这个问题,我可以使用一些指导。

rtk 版本:"@reduxjs/toolkit": "^1.6.1",

tldr;

getDefaultMiddleware() 在我的商店中不存在 + 我还没有在文档中找到任何有用的信息。

这是我得到的错误:

这是我的store.ts

import { Action, configureStore, ThunkAction } from '@reduxjs/toolkit';
import { setupListeners } from '@reduxjs/toolkit/query';
import { myApi } from './services/api';

import authorReducer from './slices/authorSlice';
import transcriptReducer from './slices/transcriptSlice';

export const store = configureStore({
  reducer: {
    [myApi.reducerPath]: myApi.reducer,
    middleware: (getDefaultMiddleware) =>
      // "This expression is not callable."
      getDefaultMiddleware().concat(myApi.middleware),
    author: authorReducer,
    transcript: transcriptReducer,
  },
});

setupListeners(store.dispatch);

// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
export type AppThunk<ReturnType = void> = ThunkAction<
  ReturnType,
  RootState,
  unknown,
  Action<string>
>;

这是我的api.ts

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';

const apiEndpoint ='http://localhost:5000';

export const myApi = createApi({
  reducerPath: 'myApi',
  baseQuery: fetchBaseQuery({ baseUrl: apiEndpoint }),
  endpoints: builder => ({
    getFileById: builder.query<any, { id: string; fileId: string }>({
      query: arg => {
        const { id, fileId } = arg;
        return `/some/endpoint/with/two/${id}/pathvariables/${fileId}`;
      },
    }),
  }),
});

// RTK Query will automatically generate hooks for each endpoint query
export const { useGetFileByIdQuery } = myApi;

【问题讨论】:

  • 不确定这是否有帮助:stackoverflow.com/a/68486869/5385381
  • 谢谢。我正在搜索文档以查看是否可以对其进行后门并使用旧的折旧方法,直到有人提出更好的建议。
  • @ksav 想通了。稍后在下面添加答案。

标签: redux react-redux next.js redux-toolkit rtk-query


【解决方案1】:

答案:

我在reducer: { ... }内部定义了我的中间件。我需要将它移到该减速器对象的外部。问题解决了。

export const store = configureStore({
  reducer: {
    [myApi.reducerPath]: myApi.reducer,
    author: authorReducer,
    transcript: transcriptReducer,
  },
  middleware: getDefaultMiddleware =>
    getDefaultMiddleware().concat(myApi.middleware),
});

【讨论】:

    猜你喜欢
    • 2021-09-10
    • 2021-11-06
    • 2022-12-17
    • 2021-11-04
    • 2022-06-22
    • 2021-12-21
    • 2023-01-22
    • 2022-07-27
    • 2022-10-08
    相关资源
    最近更新 更多