Search code examples
reactjsreact-routerreduximmutable.jsreact-router-redux

react-router-redux and redux-immutable: You cannot change <Router history>; it will be ignored


I'm trying to use react-router-redux with redux-immutable and I get an error when the @@router/LOCATION_CHANGE action is triggered:

action @ 14:19:07.625 @@router/LOCATION_CHANGE 
%c prev state color: #9E9E9E; font-weight: bold Map { "repos": Map { "loading": false, "reposCount": 0 }, "users": Map { "loading": false, "usersCount": 0 }, "router": Map { "locationBeforeTransitions": null } }
%c action color: #03A9F4; font-weight: bold { type: '@@router/LOCATION_CHANGE',
  payload: 
   { pathname: 'blank',
     search: '',
     hash: '',
     state: null,
     action: 'POP',
     key: '5b05pd',
     query: {},
     '$searchBase': { search: '', searchBase: '' } } }
%c next state color: #4CAF50; font-weight: bold Map { "repos": Map { "loading": false, "reposCount": 0 }, "users": Map { "loading": false, "usersCount": 0 }, "router": Map { "locationBeforeTransitions": Map { "pathname": "blank", "search": "", "hash": "", "state": null, "action": "POP", "key": "5b05pd", "query": Map {}, "$searchBase": Map { "search": "", "searchBase": "" } } } }
—— log end ——
<Provider> does not support changing `store` on the fly. It is most likely that you see this error because you updated to Redux 2.x and React Redux 2.x which no longer hot reload reducers automatically. See https://github.com/reactjs/react-redux/releases/tag/v2.0.0 for the migration instructions.
Warning: [react-router] You cannot change <Router history>; it will be ignored

I've been reading online at this issue seem to be caused by hot loader but I'm not using it.

This is how the code looks like:

Routes

const routes = (
    <Route path="/" component={AppLayout}>
        <IndexRoute component={Home} />
        <Route path="/users" component={UsersPage} />
        <Route path="/repos" component={ReposPage} />
    </Route>
);

Root component

class Root extends React.Component<RootComponentProps, void> {
  public render() {
    const { store, history, routes } = this.props;
    return (
      <Provider store={store}>
        <div>
          <Router history={history}>
            {routes}
          </Router>
          <DevTools />
        </div>
      </Provider>
    );
  }
}

routerReducer

const initialRouterReducerState = Immutable.fromJS({
    locationBeforeTransitions: null
});

let routerReducer = (state = initialRouterReducerState, action: any) => {
    if (action.type === LOCATION_CHANGE) {
        return state.merge({
            locationBeforeTransitions: action.payload
        });
    }
    return state;
};

main

// ...

let history = syncHistoryWithStore(browserHistory, store, {
    selectLocationState: (state: any) => state.get("routing").toJS()
});

render(
    <Root store={store} history={history} routes={routes} />,
    document.getElementById(container)
);

Do you have any ideas about what can be causing this issue?


Solution

  • It turns out that all my problems were caused by a wrong configuration of jsdom. I have created a dom.ts file that I can import from the files that contains mocha unit tests:

    /// <reference path="../src/interfaces/interfaces.d.ts" />
    
    let jsdom = require("jsdom");
    
    // setup the simplest document possible
    let doc = jsdom.jsdom(
        `<!doctype html>
        <html>
            <body>
                <div id="root"/><div>
            </body>
        </html>`,
        {
            url: "http://redux-bootstrap.com/"
        }
    );
    
    // get the window object out of the document
    let win = doc.defaultView;
    
    // set globals for mocha that make access to document and window feel 
    // natural in the test environment
    let g: any = global;
    g.document = doc;
    g.window = win;
    
    // take all properties of the window object and also attach it to the 
    // mocha global object
    propagateToGlobal(win);
    
    // from mocha-jsdom https://github.com/rstacruz/mocha-jsdom/blob/master/index.js#L80
    function propagateToGlobal (window: any) {
      for (let key in window) {
        if (!window.hasOwnProperty(key)) { continue; }
        if (key in global) { continue; }
        g[key] = window[key];
      }
    }