简体   繁体   English

在状态树中找不到路由器减速器,它必须安装在“路由器”下

[英]Could not find router reducer in state tree, it must be mounted under "router"

在此处输入图像描述

I am using these versions我正在使用这些版本

"react": "^17.0.2",
"react-dom": "^17.0.2"
"react-router-dom": "^5.2.0"
"connected-react-router": "^6.8.0"
"history": "4.10.1"

export const browserHistory = createBrowserHistory({ basename: '/clearance-authorization' }) export const browserHistory = createBrowserHistory({ basename: '/clearance-authorization' })

i am getting this Error Could not find router reducer in state tree, it must be mounted under "router"我收到此错误无法在状态树中找到路由器减速器,它必须安装在“路由器”下

reducers.js减速器.js

export default (history) => {
const appReducer = (asyncReducer) => {
  return combineReducers({
    notifications,
    router: connectRouter(history),
    ...asyncReducer
  })
}
const rootReducer = (state, action) => appReducer(state, action)
return rootReducer
}

store.js store.js

import { createBrowserHistory } from 'history'
export const history = createBrowserHistory({
  basename: '/clearance'
})
const middleware = [routerMiddleware(history), sagaMiddleware, notifications]

const configureStore = (initialState) => {
  const store = createStore(
    createReducer(history),
    initialState,
    compose(
      applyMiddleware(...middleware),
      getReduxDevTools(process.env.NODE_ENV === 'development')
    )
  )
  store.asyncReducers = {}
  store.runSaga = sagaMiddleware.run
  store.close = () => store.dispatch(END)
  return store
    }

export default configureStore

App.js应用程序.js

import configureStore, { history } from './redux/store'
import { ConnectedRouter } from 'connected-react-router'

  <Provider store={store}>
      <ConnectedRouter history={history}>
        <Frame handleScrolling={false}>
         </Frame>
      </ConnectedRouter>
    </Provider>

Issue问题

From your description and error it seems you've not added the connected router to your root reducer.根据您的描述和错误,您似乎没有将连接的路由器添加到您的根减速器。

Solution解决方案

Import connectRouter function and create the root reducer with a router key and pass the history object.导入connectRouter函数并使用router键创建根 reducer 并传递history对象。 Redux doesn't have a matching reducer, or specifically the connected-react-router selectors are attempting to select from non-existent state. Redux 没有匹配的 reducer,或者特别是connected-react-router选择器正在尝试从不存在的状态中进行选择。

Example from docs :来自文档的示例

In your root reducer file,在你的根减速器文件中,

  • Create a function that takes history as an argument and returns a root reducer.创建一个以history为参数并返回根减速器的函数。

  • Add router reducer into root reducer by passing history to connectRouter .通过将history传递给connectRouterrouter减速器添加到根减速器中。

  • Note: The key MUST be router .注意:密钥必须是router

     // reducers.js import { combineReducers } from 'redux'; import { connectRouter } from 'connected-react-router'; ... // res of your reducers const createRootReducer = (history) => combineReducers({ router: connectRouter(history), ... // rest of your reducers }); export default createRootReducer;

... ...

Import your history object and custom createRootReducer for use when creating your Redux store.导入您的历史对象和自定义createRootReducer以在创建 Redux 存储时使用。 Follow the rest of the connected-react-router docs for specifics.请遵循connected-react-router文档的其余部分以获取详细信息。

Example:例子:

import { browserHistory } from '../path/to/history';
import createRootReducer from './reducers';

...

createRootReducer(browserHistory);

changed the code in reducers reducers.js更改了 reducers reducers.js 中的代码

export default (history) => {
  return combineReducers({
    notifications,
    referenceData,
    clearance,
    lotNotes,
    siteTour,
    buyerNotes,
    router: connectRouter(history)
  })
}

暂无
暂无

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

相关问题 反应:连接反应路由器:在 state 树中找不到路由器减速器,它必须安装在“路由器”下 - React: connect-react-router: Could not find router reducer in state tree, it must be mounted under "router" 如何修复错误:在 state 树中找不到路由器减速器,它必须安装在“路由器”下 - How to fix error: Could not find router reducer in state tree, it must be mounted under “router” connectedRouter 错误:在状态树中找不到路由器减速器,它必须安装在“路由器”下 - connectedRouter Error: Could not find router reducer in state tree, it must be mounted under "router" 错误:在状态树中找不到路由器减速器,它必须安装在“路由器”下 - Error: Could not find router reducer in state tree, it must be mounted under "router" 在状态树中找不到router reducer,与redux-persist结合时必须挂载在“router”下 - Could not find router reducer in state tree, it must be mounted under "router" when combining with redux-persist 未捕获 在 state 树中找不到路由器减速器,它必须安装在“路由器”下 - Uncaught Could not find router reducer in state tree, it must be mounted under “router” 在Redux Reducer中重定向路由器 - Redirect router in redux reducer React Router重新渲染整个应用并重置Redux状态树? - React router rerenders whole app and resets redux state tree? state 在文件树减速器中未定义 - state undefined in file tree reducer React Router 选项卡——保持组件挂载 - React Router Tabs — Keep Components Mounted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM