2

When trying to run my React app I keep getting this error in the browser:

TypeError: Cannot read property 'history' of undefined

LinkContainer.js
var href = this.context.router.history.createHref(typeof to === 'string' ? {

The console shows this error:

The context `router` is marked as required in `LinkContainer`, but its value is `undefined`.

I have the <Router> defined around everything in the Index.js and have the LinkContainers setup correctly, or so I think. Then I just have one simple route at the moment. I read a lot where its React-Router v4 that causes issues when working with LinkContainer in React-Router-Bootstrap, but is there no solve for this in v4 and I should downgrade to v3 or am I totally just missing something here to allow this to work?

I am using these package versions:

"react": "^16.8.3",
"react-bootstrap": "^1.0.0-beta.5",
"react-dom": "^16.8.3",
"react-router": "^4.3.1",
"react-router-bootstrap": "^0.24.4",
"react-router-dom": "^4.4.0-beta.6",

I have the following code:

Index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { BrowserRouter as Router } from "react-router-dom";

ReactDOM.render(<Router><App /></Router>, document.getElementById("root"));

serviceWorker.unregister();

App.js

import React, { Component } from "react";
import { Navbar, Nav, NavItem } from "react-bootstrap";
import { LinkContainer } from "react-router-bootstrap";
import "./App.css";
import Routes from "./Routes";

class App extends Component {
    render() {
      return (
        <div className="App container">
          <Navbar fluid collapseOnSelect>
              <Navbar.Brand>
                <LinkContainer to="/home">
                    <NavItem>Ness</NavItem>
                </LinkContainer>
              </Navbar.Brand>
              <Navbar.Toggle />
            <Navbar.Collapse>
              <Nav pullRight>
                <LinkContainer to="/signup">
                  <NavItem>Signup</NavItem>
                </LinkContainer>
                <LinkContainer to="/login">
                  <NavItem>Login</NavItem>
                </LinkContainer>
              </Nav>
            </Navbar.Collapse>
          </Navbar>
          <Routes />
        </div>
      );
    }
}

export default App;

Routes.js

import React from "react";
import { Route, Switch } from "react-router-dom";
import Home from "./containers/Home";

export default () => 
    <Switch>
        <Route path="/home" component={Home} />
    </Switch>;
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Src1988
  • 173
  • 2
  • 14
  • 1
    If I use Link from "react-router-dom" I can get it to work, but I cant get LinkContainer to work from "react-router-bootstrap". – Src1988 Mar 01 '19 at 18:58

0 Answers0