我有一个ConnectedRouter,我想为所有路由添加哈希,所以我添加了这样的HashRouter组件:
// @flow
import React from 'react';
import { Router, Route,Switch } from 'react-router'
import { HashRouter } from 'react-router-dom'
import { ConnectedRouter } from 'react-router-redux';
import { routerActions } from 'react-router-redux';
import { UserAuthWrapper } from 'redux-auth-wrapper';
import StudiesViewContainer from './components/views/studies/StudiesViewContainer';
import NotificationsViewContainer from './components/views/notifications/NotificationsViewContainer';
import UserView from './components/views/user/UserView';
import StudyViewContainer from './components/views/studies/StudyViewContainer';
import { getUser } from './reducers';
import LoginView from './components/views/login';
import NotFoundView from './components/views/notFound';
import ForbiddenView from './components/views/forbidden';
const UserIsAuthenticated = UserAuthWrapper({
authSelector: getUser,
redirectAction: routerActions.replace,
failureRedirectPath: '/',
wrapperDisplayName: 'UserIsAuthenticated'
});
const configRouter = (history: Object) => {
return () =>
<ConnectedRouter history={ history }>
<HashRouter>
<Switch>
<Route path="/studies" component={ StudiesViewContainer } />
<Route path="/study/:id" component={ StudyViewContainer } />
<Route path="/user" component={ UserView } />
<Route path="/notifications" component={ NotificationsViewContainer } />
<Route path="/forbidden" component={ ForbiddenView } />
<Route path="/not-found" component={ NotFoundView } />
<Route path="/" component={ LoginView } />
<Route path="*" component={ NotFoundView } />
</Switch>
</HashRouter>
</ConnectedRouter>
};
export default configRouter;
问题在于当我做这样的事情时:
push('studies')
路由不添加哈希值,并且不会呈现新组件。
我将浏览器历史记录添加到我的商店,这是configureStore文件:
// @flow
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import createHistory from 'history/createBrowserHistory'
import { routerMiddleware } from 'react-router-redux';
import createSagaMiddleware from 'redux-saga'
import {
persistStore,
autoRehydrate,
createTransform
} from 'redux-persist';
import mainSaga from '../sagas';
import reducer from '../reducers';
const history = createHistory();
const routingMiddleware = routerMiddleware(history);
const sagaMiddleware = createSagaMiddleware();
// Remove error field from persisted auth substate
let authTransform = createTransform(
(inboundState, key) =>
key === 'auth' ?
{ ...inboundState, error: undefined }:
inboundState,
outboundState => outboundState,
{
whitelist: [
'auth',
'permissions'
]
}
);
const configureStore = (): Promise<*> => {
let middlewares = [routingMiddleware,thunk, sagaMiddleware ];
let composeEnhancers = compose;
if(process.env.NODE_ENV !== 'production') {
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
}
const store = createStore(
reducer,
composeEnhancers(
applyMiddleware(...middlewares),
autoRehydrate()));
sagaMiddleware.run(mainSaga);
return new Promise(resolve => {
persistStore(
store, {
whitelist: ['auth', 'permissions'],
debounce: 500,
transforms: [
authTransform
]
},
() => resolve({ store, history })
);
});
};
export default configureStore;
任何人都可以帮助我让push
按预期工作吗?
我在json包中使用以下版本的路由器:
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"react-router-redux": "next",
答案 0 :(得分:5)
我遇到了类似的问题,我使用
解决了这个问题从'history / createHashHistory '导入createHistory
而不是
从'history / createBrowserHistory '导入createHistory
在导出组件时也添加了路由器,如中所示 https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/blocked-updates.md
答案 1 :(得分:0)
我将浏览器历史记录添加到我的商店,这是configureStore文件:
最有可能的问题在于history
- 在路由器中使用的对象和在redux状态下提出的对象之间的差异。然后在redux端const history = createHistory();
创建历史记录,似乎它没有与ConnectedRouter
对象链接,但它不能跟随所谓的代码。
如果将有效history
对象传递给ConnectedRouter
,请尝试检查后续路由器操作是否会被saga
进程或其他中间件拦截。尝试按https://github.com/gaearon/redux-devtools工具继续发出行动流程。
此外,您可以尝试通过添加具有完整生命周期的根组件(继承自React.Component
类)来执行手动更新,并添加以下代码:
this.props.history.listen((location, action) => {
console.log("on route change");
});
然后您可以发现路由器状态已更改,如果采取了操作,只需在根组件中执行forceUpdate
方法。另请参阅此文档:https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/blocked-updates.md