React-Router@4:使用 React-Hot-Loader@3.beta.6 路由不能正常切换

React-Router@4: Routes not switching normally with React-Hot-Loader@3.beta.6

我正在使用 React-Router@4React-Hot-Loader@3.beta.6 以及 Webpack@2^ 构建一个 React 应用程序。

奇怪的事情发生了,

我在 routes.js 文件中声明了这样的路由

/* global __DEV__ */
import React from 'react';
import { Switch, Route, Link } from 'react-router-dom';

// import asyncRoute from './asyncRoute';
// const Welcome = asyncRoute(() => import('../views/Welcome'));
// const About = asyncRoute(() => import('../views/About'));
// const Topics = asyncRoute(() => import('../views/Topics'));

import Welcome from '../views/Welcome';
import About from '../views/About';
import Topics from '../views/Topics';

const Routes = ({ ...properties }) => {
  return (
    <div>
      <div>
        Dummy Links to check Code Splitting
        <ul>
         <li><Link to="/">Welcome</Link></li>
         <li><Link to="/about">About</Link></li>
         <li><Link to="/topics">Topics</Link></li>
       </ul>
      </div>
      <Switch>
        <Route exact path="/" component={Welcome} />
        <Route path="/about" render={(props) => <h3>About</h3>} />
        <Route path="/topics" component={Topics} />
      </Switch>
    </div>
  );
}

export default Routes;

// This will enable/force Hot Module replacement on __DEV__ environment.
// if (__DEV__) {
//   require('../views/Welcome');
//   require('../views/About');
//   require('../views/Topics');
// }

我的 Main.js 文件看起来像这样,

import React from 'react';
import ReactDOM from 'react-dom';
import { AppContainer } from 'react-hot-loader';

import configureStore from './redux/configureStore';

const initialState = window.__INITIAL_STATE__;
const store = configureStore(initialState);

import './styles/main.css';

import Root from './views/Root';

function render(Root) {
  ReactDOM.render(
    <AppContainer>
      <Root store={ store } />
    </AppContainer>,
    document.getElementById('root')
  );
}

if (module.hot) {
  module.hot.accept('./views/Root', () => {
    const nextRoot = require('./views/Root').default;
    render(nextRoot);
  });
}

render(Root);

正常的路由切换没有发生,你可以看下面的附件视频

https://share.viewedit.com/XbjKCUjbcvhcL3xRXm8kX7

从视频中看,正常情况下点击链接不会重新渲染,但当我更改某些组件中的某些内容时,react-hot-loader 补丁正在发生渲染切换路径。

我的项目在这个路径 https://github.com/bboysathish/react-boilerplate/tree/dev

我该如何解决这个问题?

实际上,结果是阻塞了上下文传播。

请执行以下差异,让您的应用程序按预期运行:

diff --git a/app/routes/Routes.js b/app/routes/Routes.js
index 2b9b787..45dbcbf 100644
--- a/app/routes/Routes.js
+++ b/app/routes/Routes.js
@@ -22,7 +22,7 @@ const Routes = ({ ...properties }) => {
          <li><Link to="/topics">Topics</Link></li>
        </ul>
       </div>
-      <Route path="/" component={Welcome} />
+      <Route path="/" exact component={Welcome} />
       <Route path="/about" component={About} />
       <Route path="/topics" component={Topics} />
     </div>
diff --git a/app/views/App.js b/app/views/App.js
index 405414c..c712c3a 100644
--- a/app/views/App.js
+++ b/app/views/App.js
@@ -1,5 +1,6 @@
 import React, { Component } from 'react';
 import { connect } from 'react-redux';
+import { withRouter } from "react-router-dom";

 class App extends Component {
   render() {
@@ -12,4 +13,4 @@ class App extends Component {
   }
 }

-export default connect()(App);
+export default withRouter(connect()(App));

让我们深入研究一下:

-export default connect()(App);
+export default withRouter(connect()(App));

这将确保您连接的组件响应上下文更改。 React-redux 不会在上下文更改时重新渲染组件,这是一个已知问题。

-      <Route path="/" component={Welcome} />
+      <Route path="/" exact component={Welcome} />

这将确保您的 Welcome 组件仅在确切的 / URL.

上呈现