Redux RTK 查询 : getDefaultMiddleware is not a function

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

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

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

tldr;

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

这是我得到的错误:

enter image description here

这是我的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;

最佳答案

回答:

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

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

关于Redux RTK 查询 : getDefaultMiddleware is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69095766/

相关文章:

javascript - Next.js 嵌套动态文件夹路由

javascript - 在不使用状态和 Action 的情况下同步两个 react 组件的滚动

JavaScript : is it possible to get data using twitter api via front-end (react)

javascript - react 导航 : 'navigation.state.params' is always undefined in 'static navigationOptions'

reactjs - 如何将输入值从子 Form 组件传递到其父组件的状态以使用 React hooks 提交?

javascript - NextJS 中的 EditorJS 无法加载插件

javascript - redux如何使用状态

javascript - 将状态从 childComp 传输到 ParentComp

javascript - 导入新的 React 组件会编译但不会渲染

javascript - 使用react-virtualized-select时出错