可在Redux观看的史诗中使用的来自“ connected-react-router”的`push`不会更改网址并呈现空白页

时间:2018-11-09 14:18:50

标签: react-router-v4 redux-observable connected-react-router

Redux可观察的史诗中使用的“ connected-react-router”中的

push不会更改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中那样进行热模块更换,但是我认为这没有关系。

更新:

我添加了史诗来收听:

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

似乎采取了以下行动:

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

``它对网址没有任何影响。

更新

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

1 个答案:

答案 0 :(得分:1)

在创建Redux商店时,您似乎已经忘记添加routerMiddleware了: https://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),
    ),
  )
}