Skip to content

React-router-bootstrap is not working with react-router 5.0.0 #250

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

Closed
kris2kris opened this issue Mar 22, 2019 · 9 comments
Closed

React-router-bootstrap is not working with react-router 5.0.0 #250

kris2kris opened this issue Mar 22, 2019 · 9 comments

Comments

@kris2kris
Copy link

kris2kris commented Mar 22, 2019

Since react-router is in version 5.0.0, react-router-bootstrap is not working.

The following error occurred LinkContainer.js:147 Uncaught TypeError: Cannot read property 'history' of undefined at LinkContainer.render (LinkContainer.js:147)

More information about this problem can be find here [(https://github.com/remix-run/react-router/issues/6630)]

for information :

@sampsakuronen We have never documented or supported using this.context.router directly. That's our private API. I discussed this in the release notes to 4.4.0-beta.0.

@kris2kris kris2kris changed the title React-router-bootstrap is not working with react-router 5.0 React-router-bootstrap is not working with react-router 5.0.0 Mar 22, 2019
@mxschmitt
Copy link
Member

I think #248 fixed that, but we have to publish a new release.
cc @jquense @taion

@ArnoSaine
Copy link

Here's a workaround until #248 is released:

import React from 'react';
import PropTypes from 'prop-types';
import { BrowserRouter, withRouter } from 'react-router-dom';

<BrowserRouter>
  <RouterContextProvider>
    // ...
  </RouterContextProvider>
</BrowserRouter>

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

@v12
Copy link
Member

v12 commented Apr 4, 2019

This issue is resolved in v0.25.0, which is already published to npm.

@batbayar-su
Copy link

Still having this issue in v0.25.0

@v12
Copy link
Member

v12 commented Jul 14, 2019

@batbayar-su could you please provide an example that reproduces the problem? Original issue was resolved and you need to elaborate what your setup is to ensure that your issue is the same.

@batbayar-su
Copy link

batbayar-su commented Jul 15, 2019

Here's a workaround until #248 is released:

import React from 'react';
import PropTypes from 'prop-types';
import { BrowserRouter, withRouter } from 'react-router-dom';

<BrowserRouter>
  <RouterContextProvider>
    // ...
  </RouterContextProvider>
</BrowserRouter>

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

It worked with this modification. @ArnoSaine could you explain to me why it is necessary, please?

@ArnoSaine
Copy link

It worked with this modification. @ArnoSaine could you explain to me why it is necessary, please?

@batbayar-su react-router-bootstrap <0.25 uses this.context.router to access React Router context. React Router v5 switched to the current context API and this.context.router is not available.

<RouterContextProvider> uses the legacy context API to provide router context (this.context.router) to its children, so it's possible to use React Router v5 with react-router-bootstrap <0.25.

@martinduo
Copy link

I have this problem using react-router-bootstrap v0.25.0 and react-router-dom v5.1.2. My environment is from CRA and I do not know how to apply the workaround shown. How and where do I add that workaround? Any example code or instructions? Is there a substitute for react-router-bootstrap I could try?

@martinduo
Copy link

Update to my last above: After stopping work on my project due to this bug, I came back 2 days later and it works with no error. Does this mean it's a timing bug? And if I had restarted at the time it would have gone away?

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

6 participants