react-route,react-hot-loader.webpack(你不能改变<router routes =“”>;它会被忽略)

时间:2016-01-13 07:33:20

标签: javascript reactjs webpack webpack-dev-server react-hot-loader

这是我使用reactreact-routerreact-hot-loaderwebpack-dev-serverwebpack的第一个项目。当我更改react组件中的代码时,热加载器变得有效,但同时,控制台告诉我一个警告:

  

您无法更改“路由器路由”;它将被忽略。

我不知道如何解决这个问题。有代码:

webpack代码:

    var path = require('path');
    var webpack = require('webpack');

    module.exports = {
      devtool: 'source-map' ,
      entry: [
        'webpack-dev-server/client?http://localhost:3000',
        'webpack/hot/only-dev-server',
        './jsx/index'
      ],
      output: {
        path: path.join(__dirname, 'public'),
        filename: 'bundle.js',
        publicPath: '/public/'
      },
      plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin()
      ],
      resolve: {
        extensions: ['', '.js', '.jsx', 'json']
      },
      module: {
        loaders: [{
          test: /\.js$/,
          exclude: /node_modules/,   
          loaders: ['react-hot', 'babel'],
          }]
      },
      watch:true
    };

索引代码:

    import React from 'react'
    import ReactDOM  from 'react-dom'
    import { Router, Route, Link } from 'react-router'
    import App from './index/app'
    import About from './index/about'
    import Inbox from './index/inbox'
    class Routers extends React.Component {
      render() {
         return ( 
            <Router>
                <Route path="/" component={App}>
                  <Route path="about" component={About} />
                  <Route path="inbox" component={Inbox} />
                </Route>
            </Router>
          );
        }
    }

ReactDOM.render(<Routers />, document.getElementById('root'));
谢谢!!!!

7 个答案:

答案 0 :(得分:10)

只有你需要做的事情就是把<Route />抛出render()方法 因此,有很多方法可以解决这个问题 最官方的方式是@Stormy说的 我的解决方案是这样的:

const routes = (
  <Route path="/" component={App}>
    <Route path="about" component={About} />
    <Route path="inbox" component={Inbox} />
  </Route>
)

// Don't let <Route> in render() method
class Routers extends React.Component {
  render() {
     return ( 
        <Router>
          { routes }
        </Router>
      );
    }
}

答案 1 :(得分:1)

尝试使用此配置 https://github.com/reactjs/react-router/issues/2704#issuecomment-170940448

  const routeConfig = [
  { path: '/:locale',
    component: App,
    indexRoute: { component: NewsCardsContainer },
    ...
  }
];
return (
  <IntlProvider key="intl" {...intlData}>
    <Router history={history} routes={routeConfig} />
  </IntlProvider>
)

答案 2 :(得分:0)

暴风雨使用<Router routes={Routes}/>的建议对我有用。这是我的警告免费代码snippits与反应热模块替换:

<强> ./ index.js

import './globals';
import React from "react";
import ReactDOM from "react-dom";
import { AppContainer as HotContainer } from "react-hot-loader";
import { browserHistory } from 'react-router';
import Routes from "./components/Routes.jsx";

const render = function() {
    let Router = require('react-router').Router;
    ReactDOM.render(
        <HotContainer>
            <Router history={browserHistory} routes={Routes}/>
        </HotContainer>,
        document.getElementById('react-container'),
    );
};

render();

if( module.hot ) {
    module.hot.accept('./components/Routes', () => {
        render();
    });
}

<强> ./组件/ Routes.jsx

import React from "react";
import { Route, IndexRoute } from "react-router";
import App from "./App.jsx";
import Graphs from "./graphs/Graphs.jsx";
import Trends from "./trends/Trends.jsx";
import Patterns from "./patterns/Patterns.jsx";

const Routes = (
    <Route path="/" component={App}>
        <IndexRoute component={Graphs}/>
        <Route path="graphs" component={Graphs}/>
        <Route path="trends" component={Trends}/>
        <Route path="patterns" component={Patterns}/>
    </Route>
);
export default Routes;

答案 3 :(得分:0)

路由器实际上永远不应该改变,因此在这种情况下你应该能够为shouldComponentUpdate()返回false。

import React from 'react'
import ReactDOM  from 'react-dom'
import { Router, Route, Link } from 'react-router'
import App from './index/app'
import About from './index/about'
import Inbox from './index/inbox'
class Routers extends React.Component {

  shouldComponentUpdate(){
     return false;
  }

  render() {
     return ( 
        <Router>
            <Route path="/" component={App}>
              <Route path="about" component={About} />
              <Route path="inbox" component={Inbox} />
            </Route>
        </Router>
      );
    }
}

答案 4 :(得分:0)

我的解决方案是改变&#34; Reflux.Component&#34;到&#34; React.Component&#34;

&#13;
&#13;
class AppRouter extends Reflux.Component {
  constructor(props){
    super(props);
    this.store = AuthStore;
  }
  requireAuth (nextState, replace, callback) {

    if (nextState.location.pathname != '/login' && nextState.location.pathname != '/logout') {
      ActionsAuth.jwt.refresh();
    }
    const token = UtilsJWT().getToken();
    if (token){
      if (nextState.location.pathname == '/login') {

        window.location.href = '/main';
      }
      callback();
    }else{
      if (nextState.location.pathname != '/login') {
      window.location.href = '/login';
      }
    }
  }
  verifyAuth (nextState, replace, callback) {
    const token = UtilsJWT().getToken();
    if (token){
      if (nextState.location.pathname == '/login') {

        window.location.href = '/main';
      }
      callback();
    }else{
      if (nextState.location.pathname != '/login') {
        window.location.href = '/login';
      }
      callback();
    }
  }

  render(){
    return (
      <Router history={browserHistory}>
          <Route path="/" component={App}>
              <IndexRoute component={Login} onEnter={ this.verifyAuth }/>
              <Route path="login" component={Login} onEnter={ this.verifyAuth }/>
              <Route path="main" component={Main} onEnter={ this.requireAuth }/>
              <Route path="logout" component={Logout} onEnter={ this.requireAuth }/>
              <Route path="local-sync" component={LocalSync} onEnter={ this.requireAuth }/>
              <Route path="*" component={Login} onEnter={ this.verifyAuth }/>
          </Route>
      </Router>
    )
  }
}
&#13;
&#13;
&#13;

答案 5 :(得分:0)

我有同样的问题,我的解决方案是更改&#34;导入{路由器,路由,链接}来自&#39; react-router&#39;&#34;来自&#39; react-router-dom&#39;&#34;&#34; import {HashRouter,Route,Link} 我的代码:

&#13;
&#13;
ReactDOM.render((
    <HashRouter>
        <div>
            <ul>
                <li><Link to="/">Home</Link></li>
                <li><Link to="/login">Login</Link></li>
            </ul>
            <hr/>

            <Route path="/" exact component={createComponent(Home)}/>
            <Route path="/login" component={createComponent(Login)}/>
        </div>
    </HashRouter>
), document.getElementById('root'));
&#13;
&#13;
&#13;

答案 6 :(得分:0)

我知道这是一个老问题,但有人可能会觉得这很有用。我尝试了很多东西,最终对我有用的是:

import React from 'react'
import ReactDOM  from 'react-dom'
import { Router, Route, Link } from 'react-router'
import App from './index/app'
import About from './index/about'
import Inbox from './index/inbox'

class Routers extends React.Component {
   private routes = (
      <Route path="/" component={App}>
          <Route path="about" component={About} />
          <Route path="inbox" component={Inbox} />
      </Route>
   );

   render() {
      return ( 
         <Router>
            {this.routes}
         </Router>
       );
    }
}