Unable to read location property undefined

I am trying to use the response-router-dom 4.0.0 library. But he sent me this error

Uncaught TypeError: Cannot read property 'location' undefined

There seems to be a problem in the browser. I used to use the 2.xx reactive router, and everything was fine. This is my index.js

import 'babel-polyfill' import React from 'react' import { Router, hashHistory } from 'react-router-dom' import { render } from 'react-dom' import { Provider } from 'react-redux' import { configureStore } from './store' import { routes } from './routes' const store = configureStore() render( <Provider store={store}> <Router history={hashHistory} routes={routes} /> </Provider>, document.getElementById('root') ) 

These are my routes.

 import React from 'react' import { IndexRoute, Route } from 'react-router-dom' import App from './containers/App' import Main from './containers/Main' import First from './containers/First' export const routes = ( <Route path='/' component={Main}> <Route path='/path' component={First} /> <IndexRoute component={App} /> </Route> ) 

And also for server side expression, I set this receive configuration

 app.get('*', function root(req, res) { res.sendFile(__dirname + '/index.html'); }); 
+5
source share
2 answers

React Router v4 is a complete rewrite and is not compatible with previous versions, as you assume in your code. With this in mind, you should not expect that you can just upgrade to the new major version (V4), and your application will work fine. You should check the documentation or return to version V2 / 3. Here is some code that should run you in the right direction

 import 'babel-polyfill' import React from 'react' import { BrowserRouter as Router, Route } from 'react-router-dom' import { render } from 'react-dom' import { Provider } from 'react-redux' import { configureStore } from './store' import App from './containers/App' import Main from './containers/Main' import First from './containers/First' const store = configureStore() render( <Provider store={store}> <Router> <Route path='/' component={Main} /> <Route path='/path' component={First} /> </Router> </Provider>, document.getElementById('root') ) 
+9
source
  • Check response-router-dom version in package.json

  • If its version is greater than 4, then import BrowserRouter in your file: -

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

    else, if its version is less than 4, then import Router: -

     import { Router, Route } from 'react-router-dom' 
+2
source

Source: https://habr.com/ru/post/1266211/


All Articles