2017-04-07 34 views
0

我試圖讓nodejs運行一個簡單的reactjs SPA,但是我的路由器無法正常工作。 我使用create-react-app創建了一個新的應用程序,並添加了npm install react-router。 我的package.json是reactjs與react-router不起作用

{ 
"name": "App", 
"version": "0.1.0", 
"private": true, 
"dependencies": { 
"react": "^15.5.0", 
"react-dom": "^15.5.0", 
"react-router": "^4.0.0" 
    }, 
"devDependencies": { 
    "react-scripts": "0.9.5" 
}, 
"scripts": { 
    "start": "react-scripts start", 
    "build": "react-scripts build", 
    "test": "react-scripts test --env=jsdom", 
    "eject": "react-scripts eject" 
} 
} 

我只是把不同的文件夾爲src /精鑄件/ 和我index.js反應的組分是

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import './index.css'; 
import App from './components/App' 
import { Router, Route, IndexRoute, hashHistory } from 'react-router' 

ReactDOM.render(
    <App />, 
    document.getElementById('root') 
); 

這可以作爲aspected。該應用程序出現並顯示來自reactjs的hello world示例。 但是,當我用路由器替換應用程序時

<Router history={ hashHistory }> 
    <Route path='/' component={ App }/> 
</Router> 

應用程序中斷。即使使用IndexRoute,該應用程序也不起作用。

將hasHistory更改爲browserHistory或createMemoryHistory不會改變任何內容。

您能否給我一個正確的方向提示,我錯過了什麼?

+0

應用程序如何中斷?你有什麼錯誤? –

+0

在控制檯中我得到一個警告,說明歷史是必需的TypeError:this.props.history是未定義的Warning:失敗的道具類型:道具'history'在'Router'中被標記爲必需的,但其值爲undefined。 在路由器(在index.js:8) – HexXx

+2

您正在以4.0版本的方式初始化react-router,檢查[用於設置react-router的4.0文檔](https://reacttraining.com/反應路由器/網絡/指南/快速啓動)或降級到舊版本的包 –

回答

0

@Rob M.你是我的英雄! 謝謝! 這就是要點。

當我將版本react-router更改爲2.8。如你所說,它工作得很好。

如果我理解正確的文檔,它的建議與上級路由器

<BrowserRouter> 
<HashRouter> 
<MemoryRouter> 
<NativeRouter> 
<StaticRouter> 

難道我得到這個正確使用反應路由器-DOM呢?