[英]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>
From your description and error it seems you've not added the connected router to your root reducer.根据您的描述和错误,您似乎没有将连接的路由器添加到您的根减速器。
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
选择器正在尝试从不存在的状态中进行选择。
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 passinghistory
toconnectRouter
.通过将
history
传递给connectRouter
将router
减速器添加到根减速器中。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.