javascript – react router v^4.0.0 Uncaught TypeError: Cannot read property location of undefined

javascript – react router v^4.0.0 Uncaught TypeError: Cannot read property location of undefined

Youre doing a few things wrong.

  1. First, browserHistory isnt a thing in V4, so you can remove that.

  2. Second, youre importing everything from react-router, it should be react-router-dom.

  3. Third, react-router-dom doesnt export a Router, instead, it exports a BrowserRouter so you need to import { BrowserRouter as Router } from react-router-dom.

Looks like you just took your V3 app and expected it to work with v4, which isnt a great idea.

Replace

import { Router, Route, Link, browserHistory } from react-router;

With

import { BrowserRouter as Router, Route } from react-router-dom;

It will start working.
It is because react-router-dom exports BrowserRouter

javascript – react router v^4.0.0 Uncaught TypeError: Cannot read property location of undefined

Ive tried everything suggested here but didnt work for me. So in case I can help anyone with a similar issue, every single tutorial Ive checked is not updated to work with version 4.

Here is what Ive done to make it work

import React from react;
import App from ./App;

import ReactDOM from react-dom;
import {
    HashRouter,
    Route
} from react-router-dom;


 ReactDOM.render((
        <HashRouter>
            <div>
                <Route path=/ render={()=><App items={temasArray}/>}/>
            </div>
        </HashRouter >
    ), document.getElementById(root));

Thats the only way I have managed to make it work without any errors or warnings.

In case you want to pass props to your component for me the easiest way is this one:

 <Route path=/ render={()=><App items={temasArray}/>}/>

Leave a Reply

Your email address will not be published.