redux-observable史诗中使用的history.push
重新加载整个应用程序,而不是仅加载新组件。 使用链接(react-router-dom)直接导航到“新/路径”非常有用,并且仅加载不同的组件。
该应用程序如下:
在应用程序配置中:
const history = createHashHistory({
hashType: "slash",
})
<Provider store={store}>
<Router history={history}>
<RootContainer />
</Router>
</Provider>
const configureStore = (): Store => {
return createStore(
rootReducer,
applyMiddleware(createEpicMiddleware(rootEpic, {
dependencies: { history },
})),
)
}
在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>, store: Store, { history }): ActionsObservable<Action> => (
action$.pipe(
ofType(SharedActions.OPEN_WINDOW),
mergeMap((action) => {
history.push(action.payload.url)
return Observable.empty()
}),
)
)
<Route>
s(来自“ react-router-dom”)渲染<Loadable>
(来自“ react-loadable”)
的package.json
"react": "^16.2.0",
"react-loadable": "^5.4.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",