赞
踩
路由的基础用法回顾, 源码study,文章首发于 docs,求个star
路由依赖于 react-router
,但是一般 pc 路由使用 react-router-dom
,它依赖于 react-router
npm i react-router-dom -S
a
标签hash
路由,BrowserRouter 是 history
路由- import { HashRouter, Route, Link } from 'react-router-dom'
- class App extends Component {
- ...
- render() {
- return (
- <div>
- <Link to="/" >首页</Link>
- <Link to="/page">Page</Link>
- {/* 配置路由规则 exact表示精确匹配,防止匹配其他页面的时候匹配到/,也就是首页*/}
- <Route path="/" exact component={Home}></Route>
- <Route path="/page" component={Page}></Route>
- <Route
- path={`/user/login`}
- render={routeProps => {
- return (
- <List name="这是render渲染" user="mondo" {...routeProps} />
- );
- }}
- ></Route>
- </div>
- );
- }
- }
-
- ReactDOM.render(
- <HashRouter>
- <App/>
- </HashRouter>,
- document.getElementById('root')
- )

Link
只配置路由,NavLink
可做导航路由,可配置activeClassName
和 activeStyle
- import { NavLink as Link } from 'react-router-dom';
-
- class Nav extends Component {
- render () {
- return (
- <ul className="nav">
- <li className="list"><NavLink to="/archive" activeClassName='active'>归档</NavLink></li>
- <li className="list"><NavLink to="/about" activeClassName='active'>关于</NavLink></li>
- </ul>
- );
- };
- }
-
- export default Nav;
在想要嵌套的组件里再次配置路由
- // User.js
- import React from 'react'
- import { Route, Link } from 'react-router-dom'
-
- const User = (props) => {
- // match 可以读取上层路由
- const { match } = props
- return (
- <div>
- <Link to={`${match.path}/login`}>登录</Link>
- <Link to="/user/reg">注册</Link>
- <Route path={`${match.path}/login`}>login</Route>
- <Route path="/user/reg">reg</Route>
- </div>
- )
- }
-
- export default User
-
- // App.js
- <Route path="/user" component={User}></Route>

props.match.params
访问- // App.js
- <Link to="/list/1/4">列表</Link>
- {/* 必须使用 component 来指定组件,不然访问不到match */}
- <Route path="/list/:id/:user" component={List}/>
-
- // List.js
- const List = (props) => {
- console.log(props.match.params) // { id: 1, user: 4 }
- return (
- <h2>
- { props.name }{ props.children }
- </h2>
- )
- }
?
后面的值,但是 router
没提供相应的方法,需要自己写一个函数来提取参数to
传递对象 state利用 props.history
来跳转路由
- // List.js
- const { history } = props
-
- const goRoute = () => {
- if (history) {
- history..push('/user')
- }
- }
- // 可以使用对象传值
- const goRoute = () => {
- if (history) {
- history..push({
- pathname: '/user',
- state: {
- id: 2
- }
- })
- }
- }

重定向使用 Switch
组件
- // ErrorPage.js
- import React from 'react'
-
- class Error extends React.Component{
- render(){
- return(
- <h1>页面404</h1>
- )
- }
- }
- export default Error
-
- // App.js
- import ErrorPage from './components/ErrorPage'
-
- class App extends React.Component {
- ...
- render() {
- ...
- {/* Switch表示如果匹配到了路由,就不再往下面匹配了,如果不写Switch,则一直会匹配到404页面 */}
- <Switch>
- {/* 配置路由规则 exact表示精确匹配,防止匹配其他页面的时候匹配到/,也就是首页*/}
- <Route path="/home" exact>Home</Route>
- <Route path="/page" component={Page}></Route>
- <Route path="/user" component={User}></Route>
- {/* 必须使用 component 来指定组件,不然访问不到match */}
- <Route path="/list/:id/:user" component={List}></Route>
- <Redirect from="/" to="/home" exact></Redirect>
- {/* 没有写path表示匹配到所有的路径 */}
- <Route component={ErrorPage} />
- </Switch>
- }
- }

由于只有 Route
包裹的组件才可以访问到 props
内部的路由信息,可以引入 withRouter
高阶组件来使内部组件也可以访问路由信息
- import React from 'react'
-
- import { withRouter } from 'react-router-dom'
-
-
- const GoBack = props => {
- console.log(props)
- const goBack = () => {
- props.history.go(-1)
- }
- return <button onClick={goBack}>返回</button>
- }
-
- export default withRouter(GoBack)
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。