gpt4 book ai didi

javascript - 向路由添加 hashrouter 使 'push' 停止渲染组件

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:30:10 24 4
gpt4 key购买 nike

我有一个 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",

最佳答案

我遇到了类似的问题,我通过使用解决了

从'history/createHashHistory'导入createHistory

代替

从“历史/createBrowserHistory”导入 createHistory

还按照建议在导出组件时添加了 withRouter https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/blocked-updates.md

关于javascript - 向路由添加 hashrouter 使 'push' 停止渲染组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47337173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com