Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-route,react-hot-loader.webpack (You cannot change <Router routes>; it will be ignored)

it's my first project that use react,react-router,react-hot-loader,webpack-dev-server and webpack. when I change the code in react component, the hot-loader become effective, but at the same time, the console tell me a warning:

You cannot change 《Router routes》; it will be ignored.

I don't know how to solve this issue.there is code:

webpack code:

    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
    };

index code:

    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'));

thank you !!!!

like image 397
Kevin_Z Avatar asked Jan 13 '16 07:01

Kevin_Z


2 Answers

Only thing you need to do, it's to throw <Route /> out of render() method.
So, there are many ways to solve this issue.
Most Official way is what @Stormy say.
My solution like this:

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>
      );
    }
}
like image 79
Yancey Ou Avatar answered Oct 19 '22 19:10

Yancey Ou


Try to use this configuration 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>
)
like image 41
Stormy Avatar answered Oct 19 '22 21:10

Stormy