React Router v4无法正常工作

时间:2017-03-16 11:47:50

标签: reactjs react-router react-router-v4

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

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)

1 个答案:

答案 0 :(得分:5)

我认为您的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指南。