react-router-redux icon indicating copy to clipboard operation
react-router-redux copied to clipboard

Getting `You cannot change <Router routes>; it will be ignored` when hot-loading

Open Sawtaytoes opened this issue 10 years ago • 62 comments

When hot-loading I am getting the warning:

You cannot change <Router routes>; it will be ignored

My process is I save a file in view with some update like I added a <span>, and it shows up on the page, but then this error shows. I checked into it, finding the commit here: https://github.com/rackt/react-router/commit/62f6ef24136b2b80472931e785a75c358e993e90

I don't know what this does. It looks like it's saying if you're on the current page and the route doesn't change, but everything else stayed the same, then warn.

Sawtaytoes avatar Jan 09 '16 03:01 Sawtaytoes

+1 Same issue.

ppalladino avatar Jan 09 '16 22:01 ppalladino

This sounds like a react-router issue. Please file there; re-open this if I'm wrong.

jlongster avatar Jan 12 '16 15:01 jlongster

+1 same here. reproduced by doing this:

const renderAll = () => {
  ReactDOM.render(
    (
      <Provider store={store}>
        <Router history={browserHistory}>
          <Route path="/" component={App}>
            <Route path="about" component={About} />
            <Route path="posts" component={Posts}>
              <Route path="/post/:nr" component={Post} />
            </Route>
            <Route path="*" component={NoMatch} />
          </Route>
        </Router>
      </Provider>
    ), document.getElementById('root')
  );
};

store.subscribe(renderAll);
renderAll();

tomasz-szymanek avatar Mar 16 '16 12:03 tomasz-szymanek

Started to get the same error.

amertak avatar Mar 16 '16 15:03 amertak

Hi, Is someone of you found solution for this error? Thanks!

haim-rubin avatar Apr 05 '16 12:04 haim-rubin

I found solution for my problem. I am using shared component for all other components as a parent (it has services and other things in constructor). The problem was, that also containers used this shared component and change of every component in app went directly to router through container. My "solution" was not to use this shared component for Containers (components defined in router routes). Hope this helps

amertak avatar Apr 05 '16 12:04 amertak

hi @amertak

I have the same issue.

I can't see what you're talking about.

Can you explain with some code snippet please ?

Best regards

lauterry avatar Apr 07 '16 18:04 lauterry

Hi @lauterry I'll try.

<Provider store={store}> <Router history={history}> <Route path="/sessions" component={Sessions} /> <Route path="/recordings" component={Recordings} /> <IndexRoute component={VisitorList}/> </Router> </Provider>

My problem was, that all the containers (Sesions, Recordings, VisitorList) had been using as a parent not Component from React, but my component called DashboardComponent

export default class DashboardComponent extends Component { ...

Also all other components in app were using DashboardComponent so if I changed something it went up to the container -> changed the router -> threw error

I have to point out that this might not be the same issue as you have.

amertak avatar Apr 08 '16 08:04 amertak

Hi all,

Same happened here and the solution was declaring the routes object outside of the render method as PlainRoutes, so it doesn't get updated. So taking @tomasz-szymanek code it would be something like:

const routes = {
    path: '/',
    component: App,
    childRoutes: [
        { path: '/about', component: About },
        {
            path: '/posts',
            component: Posts,
            childRoutes: [ { path: '/post/:nr', component: Post } ]
        },
        { path: '*', component: NoMatch}
    ]
};

const renderAll = () => {
  ReactDOM.render(
    (
      <Provider store={store}>
        <Router history={browserHistory} routes={routes} />
      </Provider>
    ), document.getElementById('root')
  );
};

store.subscribe(renderAll);
renderAll();

That way the routes list is not changed every time renderAll is executed and react-router doesn't complain.

OriolBonjoch avatar Jun 20 '16 16:06 OriolBonjoch

thanks @OriolBonjoch it works for me!

boneyao avatar Jul 01 '16 13:07 boneyao

You are god to me @OriolBonjoch

tomasz-szymanek avatar Jul 03 '16 22:07 tomasz-szymanek

The PlainRoutes approach suggested by @OriolBonjoch didn't work for me. Is there an actual working example of react-router-redux with hot reloading?

nakamorichi avatar Jul 20 '16 11:07 nakamorichi

First of all that is a warning that doesn't stop anything, its just annoying.

I think this happens because of react-redux top level Provider component. It triggers the componentWillReceiveProps in Route component even when they haven't really changed (component and path remains the same but is a new object since you created it inside render function).

Besides that I'm curious, @Kitanotori in what case you modified the routes in runtime?

OriolBonjoch avatar Jul 21 '16 14:07 OriolBonjoch

@OriolBonjoch It stops the hot reloading. The changes I make to the route components are not being hot reloaded.

I will post an example app to Github soon in order to explain the issue better.

nakamorichi avatar Jul 22 '16 05:07 nakamorichi

@OriolBonjoch It seems that I somehow got it working in the example I built. I wonder what I did differently in my main project... Have to examine further.

Here is an example of how to set up React, Redux, Saga, and routing together with Webpack, Babel, and hot reloading: https://github.com/Kitanotori/react-webpack-babel-hotreload-example

nakamorichi avatar Jul 22 '16 11:07 nakamorichi

@Kitanotori I think the reason your approach worked is because you extracted the routes into a module, so they were only declared once, instead of recreating them with every render.

I wasn't using react-router-redux, but I did have some state in my Root component for growl-style notifications, and I was getting this "cannot change Router..." warning whenever Root re-rendered.

I was able to get the warning to go away by extracting my routes. Before:

class Root extends Component {
  render() {
    return (
      <Router history={browserHistory} createElement={this.createElement}>
        <Route component={App}>
          <Route path="/" component={MainPage}/>
          <Route path="/page2" component={Page2}/>
          <Route path="/settings" component={SettingsPage}/>
        </Route>
      </Router>
    )
  }
}

After:

const routes = (
  <Route component={App}>
    <Route path="/" component={MainPage}/>
    <Route path="/page2" component={Page2}/>
    <Route path="/settings" component={SettingsPage}/>
  </Route>
);

class Root extends Component {
  render() {
    return (
      <Router history={browserHistory} createElement={this.createElement}>
        {routes}
      </Router>
    )
  }
}

dceddia avatar Aug 23 '16 15:08 dceddia

In my case the bug was the forceUpdate method called by subscribing to the redux store...

deryost avatar Sep 22 '16 12:09 deryost

In my case, the issue seems to occur when passing props to the routes. When the props change, the warning is fired.

<Router history={browserHistory}>
        <Route path='/' component={LoginComponent} />
        <Route path='/login' component={LoginComponent} />
        <Route path='/chooseCourse' component={ChooseCourseComponent} {...this.props} />
        <Route path='/doCourse/:courseID' component={DoCourseComponent} {...this.props} />
        <Route path='/doCourse/:courseID/:sectionID/:topicID' component={DoCourseComponent} {...this.props} />
      </Router>

I don't know how to get around this. Any ideas?

jeznag avatar Sep 24 '16 11:09 jeznag

I had this problem too and it turned out it was due to the same thing as @amertak had. I had a base component which all my other components inherited, so every change caused HMR to propagate up to route definitions and then subsequently the root component. Bizarre.

hiddentao avatar Oct 01 '16 10:10 hiddentao

@jeznag those properties are for Route component (like history or children)? If its not like that I would add it in ChooseCourseComponent and DoCourseComponent connect props.

OriolBonjoch avatar Oct 01 '16 11:10 OriolBonjoch