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

标签 javascript reactjs redux react-router

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

相关文章:

javascript - 当用户按回车键时,如何防止清除文本字段

javascript - 真正安全的 `eval` 使用 ES6 `Proxy` 和 `with` ?

javascript - 在 React 中呈现嵌套/线程评论

javascript - 如何延迟发送 Action (去抖动)

reactjs - React Redux 处理 DOM 元素启用/禁用的方式

javascript - 使用javascript在空格后反转数组

javascript - 如何使用 jQuery 的 on(..) 动态绑定(bind)到多个事件

javascript - 如何在 Redux 中实现缓存?

reactjs - 有没有办法使用钩子(Hook)来改进功能组件?

javascript - 如何正确地将节点从 ref 传递到上下文?