`“可从Redux观察到的史诗中使用的“ connected-react-router”推送”不会更改url并呈现空白页

问题描述 投票:2回答:1

push来自redux-observable史诗中使用的“ connected-react-router”,不会更改url并呈现空白页面。 state.router.location永远不会更改,因此我认为操作不会正确调度,但是组件不再呈现-这是我不知道的更改。

该应用程序如下:

在减速器中:

const rootReducer: Reducer<any, any> = history => combineReducers({
  router: connectRouter(history),
})

在应用程序配置中:

const history = createBrowserHistory({
  basename: ROOT_PATH,
})

<Provider store={store}>
    <ConnectedRouter history={history}>
      <RootContainer />
    </ConnectedRouter>
</Provider>

const configureStore = (): Store => {
  return createStore(
    rootReducer(history),
    applyMiddleware(createEpicMiddleware(rootEpic)),
  )
}

在RootContainer.js中

import { withRouter } from "react-router-dom"

const Root = withRouter(connect(mapStateToProps, mapDispatchToProps)(RootComponent))
export default Root

在史诗中:

import { push } from "connected-react-router"

const navigateTo = (action$: ActionsObservable<Action>): ActionsObservable<Action> => (
  action$.pipe(
    ofType(SharedActions.OPEN_WINDOW),
    mergeMap((action) => {
      return of(push(action.payload.url))
    }),
  )
)

package.json

"connected-react-router": "^5.0.1",
"react": "^16.2.0",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"redux": "^3.6.0",
"redux-observable": "^0.17.0",
"rxjs": "^5.5.7",

我没有this example中的任何热模块替换,但我认为这没有关系。

UPDATE:

我添加了史诗来收听:

import { CALL_HISTORY_METHOD, LOCATION_CHANGE, push } from "connected-react-router" 

似乎执行了以下操作:

{
  type: "@@router/CALL_HISTORY_METHOD"
  payload: {
    args: [ "new/path" ]
    method: "push"
  }
}

只是对网址没有任何影响。

UPDATE

也可以使用Link(react-router-dom)直接导航到“ new / path”,在内部组件中效果很好,因此路径是正确的。

react-router-v4 redux-observable connected-react-router
1个回答
-1
投票

似乎您在创建Redux商店时似乎忘记了添加routerMiddlewarehttps://github.com/supasate/connected-react-router#step-2

您上面的应用程序配置应为:

import { routerMiddleware } from "connected-react-router";

const history = createBrowserHistory({
  basename: ROOT_PATH,
})

<Provider store={store}>
    <ConnectedRouter history={history}>
      <RootContainer />
    </ConnectedRouter>
</Provider>

const configureStore = (): Store => {
  return createStore(
    rootReducer(history),
    applyMiddleware(
      createEpicMiddleware(rootEpic),
      routerMiddleware(history),
    ),
  )
}
© www.soinside.com 2019 - 2024. All rights reserved.