`Route`,`Switch`将重新挂载子组件,当且仅当我设置process.env.NODE_ENV ===' production'

时间:2017-11-09 16:09:36

标签: javascript reactjs redux react-router react-router-v4

  • "反应":" ^ 15.6.1",
  • " redux":" ^ 3.7.2",
  • " react-router":" ^ 4.2.0",
  • " react-router-dom":" ^ 4.2.2",

重现的步骤

// main.js

<Provider store={store}>
  <Router history={history}>
    <App>
      <Switch>
        <Redirect key='index' from='/' exact to='home'/>
        {
          _.map(routes, (route, i) => {
            const {store, path, component, exact, redirectTo} = route;
            if (redirectTo) {
              return <Redirect key={path} path={path} to={redirectTo}/>;
            }
            let routeConfig = {
              path,
              exact,
              render: props => {
                return (
                  <RouteBundle load={component}>
                    {Comp => Comp ? (<Comp {...props} />) : null}
                  </RouteBundle>
                );
              }
            };

            return <Route key={path} {...routeConfig}/>;
          })
        }
        <Route key='notFound' component={NotFound}/>
      </Switch>
    </App>
  </Router>
</Provider>

对于Route组件将创建子组件的新实例,它来自浏览器的Endless Loop重绘。我知道由<App>组件的错误位置引起的BUG。

但是,只有当process.env.NODE_ENV的值为生产时,BUG才会重现。 WHY ???

// in webpack file

new webpack.DefinePlugin({'process.env.NODE_ENV': JSON.stringify('production')}),

0 个答案:

没有答案