反应路由器 v4 不工作

React Router v4 not working

我有以下路由器设置代码:

import React from 'react'; 
import { Router, Route, browserHistory } from 'react-router'; 
import App from './App'; 

export default () => ( 
  <Router history={browserHistory}> 
    <Route name="demo" path="/mydemo" component={App} /> 
  </Router>
);

但是,我收到以下错误:

Warning: Failed prop type: The prop `history` is marked as required in `Router`, but its value is `undefined`. in Router (at Router.js:12)    
Uncaught TypeError: Cannot read property 'location' of undefined at new Router (Router.js:43)

我认为您可能混淆了 React-Router v3 和 v4 实现。也许您最近升级了?

无论如何,请尝试:

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

...

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

如果您还没有安装,请不要忘记安装 react-router-dom。另外,如果您需要更多信息,请查看官方 Quick Start 指南。