2017-08-11 34 views
1

我有這個文件在我的reactapp中導致錯誤。無法獲取未定義或空引用的屬性「位置」ReactJS

import React from 'react'; 
import { Router, Route } from 'react-router'; 

import App from './components/App'; 
import About from './components/About'; 
import NotFound from './components/NotFound'; 

const Routes =() => (
    <Router> 
     <Route path="/" component={App} /> 
     <Route path="/about" component={About} /> 
     <Route path="*" component={NotFound} /> 
    </Router> 
); 


export default Routes; 

出於某種原因,如果我更換

<Router> 
    <Route path="/" component={App} /> 
    <Route path="/about" component={About} /> 
    <Route path="*" component={NotFound} /> 
</Router> 

只是一個簡單的內容,如<div> hi </div>話,我可以看到我的應用程序嗨,否則我得到這個錯誤。

TypeError: Unable to get property 'location' of undefined or null reference

回答

1

根據您的路線結構,我假設您正在使用react-router V2或V3。

你錯過了與路由器兼用hashHistorybrowerHistory歷史的一部分,並把它定義是這樣的:

import { Router, Route, hashHistory } from 'react-router' 

<Router history={hasHistory}> 
    .... 
</Router> 

檢查DOC有關歷史更多細節。

2

我想你是使用反應路由器V4.0,使用v3.0.0它會工作。

相關問題