Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

You cannot change <Router history> #62

Open
kristian2x opened this issue Feb 6, 2020 · 0 comments
Open

You cannot change <Router history> #62

kristian2x opened this issue Feb 6, 2020 · 0 comments

Comments

@kristian2x
Copy link

kristian2x commented Feb 6, 2020

I am getting the following warning and hot-reloading is broken since implementing piwik-react-router:

backend.js:6 Warning: You cannot change Router history

This is my App.js where I call connectToHistory()

import React, { Component } from "react";
import PropTypes from "prop-types";

import { withRouter, Router, Switch, Route } from "react-router-dom";
import { createBrowserHistory } from "history";
import { library } from "@fortawesome/fontawesome-svg-core";
import MatomoTracker from "piwik-react-router";

import {
...
} from "@fortawesome/free-solid-svg-icons";
import { Button } from "react-bootstrap";
import { bootstrapUtils } from "react-bootstrap/lib/utils";
import { hot } from "react-hot-loader";

import Login from "./pages/login/Login";
import Main from "./Main";
import ProtectedRoute from "../routes/ProtectedRoute";

library.add(
...
);

bootstrapUtils.addStyle(Button, "ace");

export const matomo = MatomoTracker({
  url: "analytics.ace",
  siteId: 1
});

class App extends Component {
  render() {
    let history = createBrowserHistory();
    if (process.env.mode === "production") {
      history = matomo.connectToHistory(createBrowserHistory());
    }
    // We need this as long as react-router-bootstrap is not updated
    // to be compatible with react-router-bootstrap v5
    // see for more info: https://github.com/react-bootstrap/react-router-bootstrap/issues/250
    const RouterContextProvider = withRouter(
      class extends React.Component {
        static childContextTypes = {
          router: PropTypes.object
        };

        getChildContext() {
          const { children, ...router } = this.props;
          return { router };
        }

        render() {
          return this.props.children;
        }
      }
    );

    return (
      <>
        <Router history={history}>
          <RouterContextProvider>
            <Switch>
              <Route component={Login} exact path="/login" />
              <ProtectedRoute component={Main} path="/" />
            </Switch>
          </RouterContextProvider>
        </Router>
      </>
    );
  }
}

export default hot(module)(App);

What could be causing this? Let me know if you need any other info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant