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

发布于 2025-01-24 10:54:08 字数 1772 浏览 2 评论 0原文

我正在使用这些版本

"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"

导出const browserhistory = create browserhistory({{ Basename:“/清除授权” })

我遇到此错误在状态树中找不到路由器还原器,必须将其安装在“路由器”

reducers.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

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下

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

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

enter image description here

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'
})

i am getting this Error Could not find router reducer in state tree, it must be mounted under "router"

reducers.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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨后彩虹 2025-01-31 10:54:08

从您的描述和错误中出发

,似乎您没有将连接的路由器添加到根还原器中。

解决方案

导入ConnectRouter函数,并使用路由器键创建root还原器,然后传递历史记录对象。 Redux没有匹配的还原器,或者特别是连接的反应 - 曲线选择器正在尝试从不存在的状态中进行选择。

from docs

在您的根还原文件中,

  • 创建一个以历史为参数的函数,并返回root还原器。

  • 添加路由器通过传递历史> connectrouter 。

  • 注意:键必须为路由器

      // reducers.js
    从'redux'导入{combinereducers};
    从'connected-react-router'导入{ConnectRouter};
    ... //还原器
    
    const createRootReducer =(历史)=&gt;组合器({
      路由器:ConnectRouter(历史),
      ... //其余的还原器
    });
    
    导出默认的createrootReducer;
     

...

导入您的历史记录对象和自定义createrootredeDucer在创建Redux商店时使用。请按照连接的反应路由器文档进行细节。

例子:

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

...

createRootReducer(browserHistory);

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. Redux doesn't have a matching reducer, or specifically the connected-react-router selectors are attempting to select from non-existent state.

Example from docs:

In your root reducer file,

  • Create a function that takes history as an argument and returns a root reducer.

  • Add router reducer into root reducer by passing history to connectRouter.

  • Note: The key MUST be 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. Follow the rest of the connected-react-router docs for specifics.

Example:

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

...

createRootReducer(browserHistory);
只为一人 2025-01-31 10:54:08

更改了还原器中的代码
reducers.js

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

changed the code in reducers
reducers.js

export default (history) => {
  return combineReducers({
    notifications,
    referenceData,
    clearance,
    lotNotes,
    siteTour,
    buyerNotes,
    router: connectRouter(history)
  })
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文