简体   繁体   English

redux 工具包 +connected-react-router

[英]redux toolkit +connected-react-router

I'm trying to set up redux with connected-react-router, but I didn't really understand how to do that, I'm using the create-react-app --template redux, but if I try to do that I get the following error: Uncaught Error: Actions must be plain objects. Use custom middleware for async actions.我正在尝试使用 connected-react-router 设置 redux,但我真的不明白该怎么做,我正在使用 create-react-app --template redux,但如果我尝试这样做,我得到以下错误: Uncaught Error: Actions must be plain objects. Use custom middleware for async actions. Uncaught Error: Actions must be plain objects. Use custom middleware for async actions.

   import { configureStore, combineReducers } from "@reduxjs/toolkit";
    import { connectRouter, routerMiddleware } from "connected-react-router";
    import { createBrowserHistory } from "history";
    import homePageSlice from "./reducers/homepage/homePageSlice";
    
    export const history = createBrowserHistory();
    
    
    export const store = configureStore({
      reducer: {
        router: connectRouter(history),
        homepage: homePageSlice,
      },
      middleware: [routerMiddleware(history)],
    });

setting middleware property will override the default middleware that configureStore injects.设置中间件属性将覆盖 configureStore 注入的默认中间件。

Use this:用这个:

middleware: [
        actionToPlainObject,
        routerMiddleware(history)
    ]

and create actionToPlainObject.ts并创建 actionToPlainObject.ts

import { Middleware } from '@reduxjs/toolkit';
import IStoreState from '../IStoreState';

export const actionToPlainObject: Middleware<IStoreState, any> = (store) => (
    next
) => (action) => {
    if (!action) {
        return;
    }

    return next({ ...action });
};

If using class instances this allows to be passed to Redux.如果使用 class 实例,则允许将其传递给 Redux。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Redux Sagas 未使用 redux persist 和 connected-react-router 进入 - Redux Sagas not entered with redux persist and connected-react-router 使用已连接反应路由器在React + Redux中使用pushState - pushState in React + Redux using connected-react-router Redux-loop和Connected-React-Router是否兼容? - Are `redux-loop` and `connected-react-router` compatible? 如何使用 connected-react-router 重置 Redux 存储的状态? - How to reset the state of a Redux store using connected-react-router? 使用 react-router-dom、redux、apollo-client、connected-react-router 和 redux-persist 进行路由 - Routing with react-router-dom, redux, apollo-client, connected-react-router and redux-persist 从react-redux connect()内部连接到反应路由器push()时,位置没有变化 - No location change upon connected-react-router push() from within the react-redux connect() 连接反应路由器做什么? - What does connected-react-router do? 安装了react-redux和redux模块的连接反应路由器无法解析模块&#39;react-redux&#39; - Can't resolve module 'react-redux' in connected-react-router with react-redux and redux modules installed 如何使用 redux-thunk 调度操作让连接反应路由器重定向? - How do I get connected-react-router to redirect using a redux-thunk dispatch action? Redux-Persist和Connected-React-Router:TypeError:无法读取undefined的属性&#39;location&#39; - Redux-Persist and Connected-React-Router: TypeError: Cannot read property 'location' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM