9

I am new to React and I am building my first application with React, Redux, and React Router, so far I have successfully set up my boiler plate, however when I want to use React-Router and create routes I get the following error

TypeError: Cannot read property 'string' of undefined
./node_modules/react-router-dom/es/BrowserRouter.js
node_modules/react-router-dom/es/BrowserRouter.js:38
  35 | }(React.Component);
  36 | 
  37 | BrowserRouter.propTypes = {
> 38 |   basename: PropTypes.string,
  39 |   forceRefresh: PropTypes.bool,
  40 |   getUserConfirmation: PropTypes.func,
  41 |   keyLength: PropTypes.number,

This only happens when I try to import and use

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

Here is my index.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

import { Provider } from 'react-redux';
import store from './store'

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

import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

class Hello extends Component {
  render(){
    return(
      <div>Hello</div>
    )
  }
}


class GoodBye extends Component {
  render(){
    return(
      <div>GoodBye</div>
    )
  }
}

ReactDOM.render(<Provider store={store}>
  <BrowserRouter>
    <Route path="/hello" component={Hello}/>
    <Route path="/goodbye" component={GoodBye}/>
  </BrowserRouter>
  </Provider>, document.getElementById('root'));
registerServiceWorker();

And my package.json

{
  "name": "reactreduxrouterblog",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "axios": "^0.16.2",
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "react-redux": "^5.0.6",
    "react-router": "^2.0.0-rc5",
    "react-router-dom": "^4.0.0",
    "redux": "^3.7.2",
    "redux-logger": "^3.0.6",
    "redux-promise-middleware": "^4.4.1",
    "redux-thunk": "^2.2.0"
  },
  "devDependencies": {
    "react-scripts": "1.0.14"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

Any knowledge will be welcomed thanks!

2
  • 1
    Looking at node-modules/react-router-dom/es/BrowserRouter.js, I see import PropTypes from 'prop-types'; as one of the first lines. Can you make sure this line exists? Commented Oct 11, 2017 at 19:50
  • 5
    This has been fixed in react-router-dom ^4.2.2 Commented Jan 7, 2018 at 9:23

4 Answers 4

8

Update your react-router-dom in package.json: react-router-dom ^4.2.2

Sign up to request clarification or add additional context in comments.

Comments

4

Sometimes it may cause due to incompatible versions of "react-router-dom" and "react-router-prop-types". For me it worked with following configuration.

react-router-dom:"^4.2.2",
react-router-prop-type:"^3.2.1"

Comments

2

Try adding the PropTypes package to your app.

basename: PropTypes.string

Looks like you are just missing that package.

EDIT:

Be sure to run npm install to install all dependencies.

2 Comments

I installed the package and I still get the same error
Did you run an npm install?
2
import React from 'react';              <--as normal
import PropTypes from 'prop-types';     <--add this as a second line


App.propTypes = {
    monkey: PropTypes.string,           <--omit "React."
    cat: PropTypes.number.isRequired    <--omit "React."
};


//Wrong:  React.PropTypes.string
//Right:  PropTypes.string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.