React router
延續上一章
先npm install react-router
1.接著將我們的client/client.js 改為如下
import React from 'react'
import { render } from 'react-dom'
import App from '../components/App'
import Proptest from '../components/Proptest'
import TextDisplay from '../components/TextDisplay'
import { Router, Route, hashHistory } from 'react-router'
render((
<Router history={hashHistory}>
<Route path="/" component={App}/>
<Route path="/Proptest" component={Proptest}/>
<Route path="/TextDisplay" component={TextDisplay}/>
</Router>
),document.getElementById('app'))即可看到現在可以再url輸入http://localhost:3000/#/about
進入另一個元件
2.Link
接著到App.js加上
即可看到點選li跳至不同元件,及更改了url
3.我們現在想讓App.js變成一個nav然後點選後App.js不動,在他的下面render不同的component,所以我們要把route改為巢狀
client.js
App.js
,因為今天兩個component在client.js下為App.js的子代,所以要用{this.props.children}去顯示
(更改後記得重新整理)
4.
幫link 加上active時的style
另一種方式是幫他寫上class
activeClassName="active"
然後即可加入css檔案 和正常的class一樣
5.像Express 使用參數url
新增一個元件Repo.js
更改Client.js
之後在url輸入http://localhost:3000/#/repo/this/is 即可
PS:巢狀route,當url是子代時,會把所有的父代component都render出
6.給route 一個預設的頁面
建立home component
Home.js
App.js
(not understand now)另一個標籤(IndexRoute)
https://github.com/reactjs/react-router-tutorial/tree/master/lessons/08-index-routes
https://github.com/reactjs/react-router/blob/master/docs/guides/IndexRoutes.md
7.消除原本在url上的#
#將hash history改為browser history
App.js
之後點選link發現url後不再出現#,但點選後這時按F5,發現出現了Cannot get
只要把server.js
改為
即可
參考: https://github.com/reactjs/react-router/blob/master/docs/guides/Histories.md#configuring-your-server
參考至
https://github.com/reactjs/react-router-tutorial/tree/master/lessons/02-rendering-a-route
React router Version 4, 5 用法
記得要在/的前面加上exact,不然所有路由都會顯示Lobby畫面。
1.換頁可用如下
然後在aa裡面可用
hook 版本
2.監聽路由
巢狀路由
有些 component 想共用 header, footer 有些不共用。
記得把 / 放在最下面,不然 /fast 也會 match 到,另外記得不能在 / 加上 exact,不然 APP 下面的 component route 會無法讀取
App.js
這邊記得要在 / 加上 exact 不然 /post 也會讀取 Body component
Last updated
Was this helpful?