Router入门0x204: react-route简单栗子

news/2024/7/8 11:18:34

0x000 概述

这一章仔细讲一讲 react-route 的使用栗子

0x001 简单使用

  • 源码

    import React from 'react'
    import ReactDom from 'react-dom'
    import {BrowserRouter, Switch, Route, Link, withRouter} from 'react-router-dom'
    
    // 简单使用
    class App extends React.Component {
        render() {
            return (
                <div>
                    <div>
                        <Link to='/index'>首页</Link>
                        <Link to='/article'>文章</Link>
                        <Link to='/mine'>我的</Link>
                    </div>
                    <hr/>
                    <Switch>
                        <Route path='/index' component={() => ({render: () => <p>首页</p>})}></Route>
                        <Route path='/article' component={() => ({render: () => <p>文章</p>})}></Route>
                        <Route path='/mine' component={() => ({render: () => <p>我的</p>})}></Route>
                    </Switch>
                </div>
            )
        }
    }
    
    let MyApp = withRouter(App)
    
    ReactDom.render(
        <BrowserRouter>
            <MyApp/>
        </BrowserRouter>,
        document.getElementById('app')
    )
  • 效果

    clipboard.png

0x002 带导航激活效果

  • 源码

    import React from 'react'
    import ReactDom from 'react-dom'
    import {BrowserRouter, Switch, Route, NavLink, withRouter} from 'react-router-dom'
    import './App.css'
    // 带导航效果
    class App extends React.Component {
        render() {
            return (
                <div>
                    <div>
                        <NavLink  to='/index' activeStyle={{ color:'#eeeeee'}} >首页</NavLink>
                        <NavLink to='/article'  activeClassName='active'>文章</NavLink>
                        <NavLink to='/mine'  activeStyle={{ color:'#eeeeee'}} isActive={(match,location)=>location.pathname==='/mine'}>我的</NavLink>
                    </div>
                    <hr/>
                    <Switch>
                        <Route path='/index' component={() => ({render: () => <p>首页</p>})}></Route>
                        <Route path='/article' component={() => ({render: () => <p>文章</p>})}></Route>
                        <Route path='/mine' component={() => ({render: () => <p>我的</p>})}></Route>
                    </Switch>
                </div>
            )
        }
    }
    
    let MyApp = withRouter(App)
    
    ReactDom.render(
        <BrowserRouter>
            <MyApp/>
        </BrowserRouter>,
        document.getElementById('app')
    )
  • 效果

    clipboard.png

  • 说明
    NavLink: Link增强版,如果当前路由命中,将会启用activeStyle或者activeClassName

    - activeStyle:?Object: 当链接激活的时候的样式
    - activeClassName:?String: 当链接激活的时候的样式类
    - isActive?Function: 可以手动判断该链接是否激活,该函数的签名是:function(match, location):boolean

    0x003 重定向

  • 源码

import {BrowserRouter, Switch, Route, NavLink, Redirect, withRouter} from 'react-router-dom'

class App extends React.Component {
    render() {
        return (
            <div>
                <div>
                    <NavLink to='/index'>首页</NavLink>
                    <NavLink to='/article'>文章</NavLink>
                    <NavLink to='/mine'>我的</NavLink>
                </div>
                <hr/>
                <Switch>
                    <Route path='/index' component={() => ({render: () => <p>首页</p>})}></Route>
                    <Route path='/article' component={() => ({render: () => <p>文章</p>})}></Route>
                    <Route path='/mine' component={() => ({render: () => <p>我的</p>})}></Route>
                    <Redirect from="/" to="/index"/>
                </Switch>
            </div>
        )
    }
}
```
  • 效果
    当我们访问http://localhost:8081/时,会自动跳转到http://localhost:8081/index

0x004 没找到匹配的路由

  • 源码

    import {BrowserRouter, Switch, Route, NavLink, withRouter} from 'react-router-dom'
    
    class App extends React.Component {
        render() {
            return (
                <div>
                    <div>
                        <NavLink to='/index'>首页</NavLink>
                        <NavLink to='/article'>文章</NavLink>
                        <NavLink to='/mine'>我的</NavLink>
                    </div>
                    <hr/>
                    <Switch>
                        <Route path='/index' component={() => ({render: () => <p>首页</p>})}></Route>
                        <Route path='/article' component={() => ({render: () => <p>文章</p>})}></Route>
                        <Route path='/mine' component={() => ({render: () => <p>我的</p>})}></Route>
                        <Route component={() => ({render: () => <p>未找到这个页面</p>})}/>
                    </Switch>
                </div>
            )
        }
    }
  • 效果

    clipboard.png

0x005 url传参

  • 源码

    import {BrowserRouter, Switch, Route, NavLink, withRouter} from 'react-router-dom'
    
    class Article extends React.Component{
        render(){
            return(<p>{this.props.match.params.id}</p>)
        }
    }
    class App extends React.Component {
        render() {
            return (
                <div>
                    <div>
                        <NavLink to='/index'>首页</NavLink>
                        <NavLink to='/article/1'>文章1</NavLink>
                        <NavLink to='/article/2'>文章2</NavLink>
                        <NavLink to='/mine'>我的</NavLink>
                    </div>
                    <hr/>
                    <Switch>
                        <Route path='/index' component={() => ({render: () => <p>首页</p>})}></Route>
                        <Route path='/article/:id' component={Article}></Route>
                        <Route path='/mine' component={() => ({render: () => <p>我的</p>})}></Route>
                    </Switch>
                </div>
            )
        }
    }
    
    
  • 效果
    clipboard.png
  • 说明
    声明Route的时候使用:${name}表示要作为动态参数,之后可以通过this.props.match.params.${name}获取

0x006 页面手动跳转并传参

  • 源码

    class Article extends React.Component{
        render(){
            console.log(this)
            return(<p>文章 id:{this.props.location.state.id}</p>)
        }
    }
    class App extends React.Component {
        render() {
            return (
                <div>
                    <div>
                        <button className='btn btn-primary' onClick={()=>{this.props.history.push('/article',{id:1})}}>文章1</button>
                        <button className='btn btn-primary'  onClick={()=>{this.props.history.push('/article',{id:2})}}>文章2</button>
    
                    </div>
                    <hr/>
                    <Switch>
                        <Route path='/article' component={Article}></Route>
                    </Switch>
                </div>
            )
        }
    }
    
  • 效果

    clipboard.png

  • 说明:

    • 跳转:this.props.history.push(path:String,data:?Object)
    • 获取参数:this.props.location.state

0x007 何时使用Switch

做个试验,假设我们有两个路由:

class App extends React.Component {
    render() {
        return (
            <div>
                <Route path='/article' component={()=><p>article</p>}></Route>
                <Route path='/:name' component={()=><p>:name</p>}></Route>
            </div>
        )
    }
}

此时看效果

clipboard.png

会发现两个都命中了,这个时候可以使用Switch,他只会命中第一个命中的路由

class App extends React.Component {
    render() {
        return (
            <div>
                <Switch>
                    <Route path='/article' component={() => <p>article</p>}></Route>
                    <Route path='/:name' component={() => <p>:name</p>}></Route>
                </Switch>
            </div>
        )
    }
}
  • 命中/article
    clipboard.png
  • 命中/:name
    clipboard.png

0x008 资源

  • 源码

http://www.niftyadmin.cn/n/1933749.html

相关文章

2018-2019-2 20165235《网络对抗技术》Exp8 Web基础

2018-2019-2 20165235《网络对抗技术》Exp8 Web基础 实践过程记录&#xff1a; (1).Web前端HTML 能正常安装、启停Apache。理解HTML&#xff0c;理解表单&#xff0c;理解GET与POST方法,编写一个含有表单的HTML。 直接使用service apache2 start命令可打开Apache服务在浏览器输…

浏览器中的诈骗广告,你留意过吗?

2015年&#xff0c;推送通知刚刚出现在浏览器上&#xff0c;很少有人想知道这个工具将来会如何使用。它曾经是一种有用的技术&#xff0c;可以让经常阅读它的读者了解最新消息&#xff0c;但如今它经常被用来向网站访问者发送未经请求的广告。 受访者最终订阅了广告&#xff0…

知识点049-supervisor

2019独角兽企业重金招聘Python工程师标准>>> supervisor安装 cd /usr/local/src wget https://pypi.python.org/packages/7b/17/88adf8cb25f80e2bc0d18e094fcd7ab300632ea00b601cbbbb84c2419eae/supervisor-3.3.2.tar.gz tar -zxvf supervisor-3.3.2.tar.gz cd supe…

阿里云服务器可视化

### 阿里云ECS&#xff0c;Ubuntu Server 16.04安装图形界面远程控制 - ##### 下面教你怎么安装一个超级高效的图形界面**xfce**。 - **首先要连接上你的服务器**&#xff0c;然后安装vncserver&#xff0c;这个是用来远程连接用的。命令如下 apt-get install vnc4server - **安…

GIT安装包备用地址

如果官网下载被禁止&#xff0c;可在下面这个地址下载&#xff0c;速度飞快 http://www.wmzhe.com/soft-38801.html#download转载于:https://www.cnblogs.com/haizine/p/10882331.html

洛谷P2564 [SCOI2009]生日礼物(单调队列)

传送门 准确的来说这个应该叫尺取法&#xff1f; 先对所有的点按$x$坐标进行排序 我们维护两个指针$l,r$&#xff0c;每一次令$r$不断右移直到所有颜色齐全&#xff0c;再不断右移$l$直到颜色数不足&#xff0c;那么此时$[l-1,r]$这个区间里的彩珠肯定能构成所有颜色&#xff0…

MySQL五大引擎之间的区别和优劣之分

MyISAM&#xff1a; 创建一个myisam存储引擎的表的时候回出现三个文件 1.tb_demo.frm&#xff0c;存储表定义&#xff1b; 2.tb_demo.MYD&#xff0c;存储数据&#xff1b; 3.tb_demo.MYI&#xff0c;存储索引。 MyISAM表无法处理事务&#xff0c;这就意味着有事务处理需求的…

物联网信息泄露又一例!家用智能设备还安全吗?

今年圣诞节&#xff0c;很多人在他们的树下或袜子里找到了全新的Wyze监控摄像头。但在为收到礼物而开心的同时&#xff0c;别忘了该公司在圣诞节为每个使用其产品的人带来了一个大麻烦。据证实&#xff0c;Wyze摄像头数据泄露事件确有发生&#xff0c;该事件可能在几周时间里暴…