开发者问题收集

React-Router 无法读取未定义的属性字符串

2017-10-11
3071

我是 React 新手,我正在使用 React、Redux 和 React Router 构建我的第一个应用程序,到目前为止,我已经成功设置了我的样板,但是当我想使用 React-Router 并创建路由时,我收到以下错误

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,

这仅在我尝试导入和使用时发生

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

这是我的 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();

和我的 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"
  }
}

任何知识都将受到欢迎,谢谢!

3个回答

package.json 中更新你的 react-router-dom :react-router-dom ^4.2.2

Manuel Bautista
2018-05-09

有时,这可能是由于“react-router-dom”和“react-router-prop-types”版本不兼容造成的。对我来说,它适用于以下配置。

react-router-dom:"^4.2.2",
react-router-prop-type:"^3.2.1"
Tharindu Hewawitharana
2018-04-27

尝试将 PropTypes 包添加到您的应用中。

basename: PropTypes.string

看起来您只是缺少该包。

编辑:

确保运行 npm install 来安装所有依赖项。

Chase DeAnda
2017-10-11