Nikhil
Nikhil

Reputation: 374

React-Router-dom :- Cannot read property 'string' of undefined at Object.../node_modules/react-router-dom/es/BrowserRouter.js

I have used create-react-app for creating a new working repository for react and i wanted to add some Routing paths using react-router-dom.

This is my index.js file.

import React from 'react';
import ReactDOM from 'react-dom';
import PostsIndex from './components/posts_index';
import {BrowserRouter,Switch,Route} from 'react-router-dom';

ReactDom.render(
<BrowserRouter>
<div>
<Switch>
<Route path="/" component={PostsIndex} />
</Switch>
</div>
</BrowserRouter>,
document.getElementById('root')
);

And this is my posts_index.js file

import React,{Component} from 'react';

class PostsIndex extends Component {
render(){
return(
<div>Posts Index </div>
)
}
export default PostsIndex;

Can someone help me with this error?

Upvotes: 0

Views: 1067

Answers (3)

Olumide
Olumide

Reputation: 430

This was how I fixed this particular error at my end.

  • I updated my yarn version to the latest (v1.7.0 at this time) by downloading the installer from yarn's website
  • Then I ran yarn add react-router-dom --save
  • Restarted my application with yarn start

And away the error went. If you use npm for dependency management, I believe the same should apply while replacing yarn with npm.

Upvotes: 1

Nikhil
Nikhil

Reputation: 374

It's fixed. I just updated my react-router-dom from 4.0.0 to 4.2.2.

Upvotes: 2

ReyHaynes
ReyHaynes

Reputation: 3102

Seems to be working on my end.

Only things I had to adjust was the ReactDom to ReactDOM and a missing closing parenthesis in posts_index.js

Working solution: https://codesandbox.io/s/nkvjqrvpwj

Upvotes: -1

Related Questions