React Router

一、简介

react router是一个构建基于react应用的路由管理库。允许你在程序中定义不同的路由和导航规则。以实现不同的url路径显示不同的组件。

二、相关技术

javascript 复制代码
<Router>
    <div>
        <ul id = "menu">
            <li><Link to = "/home">Home</Link></li>
            <li><Link to = "/hello">Hello</Link></li>
            <li><Link to = "/about">About</Link></li>
        </ul>
        
        <div id = "page-container">
            <Route path = "/home" component = {Home} />
            <Route path = "/hello" component = {Hello} />
            <Route path = "/about" component = {About} />
        </div>
    </div>
</Router>

Link实现导航跳转

Route 配置路由定义,对应组件会填充父组件。

React Router API

Link -> <Link to = '/about'>About</Link> 普通连接,不会触发浏览器刷新

NavLink -> <NavLink to = '/about' activieClassName = "selected">About</NavLink >

Prompt ->

javascript 复制代码
 <Prompt when = {fromIsHalfFilledOut}  message = "Are you sure you want to leave"/>

Redirect ->

javascript 复制代码
 <Route  exact path = "/" 
  render = {() => (loggedIn ? (<Redirect to "/dashboard"/>):
 (<PublicHomePage/>))} />

url传参

react 组件可以通过 match props 获取Route 中url对应的占位符值。

javascript 复制代码
// 通过match属性获取Route Path中的占位符值
const Topic = ({match}) => (
    <h1>Topic {match.params.id}</h1>
); 

export default class RouterParams extends React.PureComponent{
    render(){
        return (
            <Router>
                <div>
                    <ul id = "menu">
                        <li><Link to = '/topic/1'>Topic 1</Link></li>
                        <li><Link to = '/topic/2'>Topic 2</Link></li>
                        <li><Link to = '/topic/3'>Topic 3</Link></li>
                    </ul>
                    <div id = "page-container">
                        <Route path = "/topic/:id" component = {Topic} />
                    </div>
                </div>
            </Router>
        );
    
    }

}

默认情况下,直接修改浏览器地址是不会触发跳转的,必须通过Link或者其它React routeApi实现跳转。

嵌套路由

1.每个React组件都是路由容器。

2.React Router的声明式语法可以方便的定义嵌套路由。

举个多级目录嵌套路由例子

javascript 复制代码
// 一级目录
export const Category = () => {
    return () => {
        <Router>
            <div>
                <ul id = "menu">
                    <li><Link to = "/category/1">Category 1</Link></li>
                    <li><Link to = "/category/2">Category 2</Link></li>
                    <li><Link to = "/category/3">Category 3</Link></li>
                </ul>
                <div id = "nav-container">
                    <Route path = "/category/:id" component = {SubCategory}>
                </div>
            </div>
        </Router>
    }
}


// 二级目录
export const SecondCategory = ({match}) => {
    return () => {
        <Router>
         <div>
           <ul id = "menu">
             <li><Link to = "/category/${match.params.id}/sub/1">Category 1</Link></li>
             <li><Link to = "/category/${match.params.id}/sub/2">Category 2</Link></li>
             <li><Link to = "/category/${match.params.id}/sub/3">Category 3</Link></li>
           </ul>
           <div id = "page-container">
             <Route path = "/category/:id/sub/:subId" component = {Page}>
           </div>
         </div>            
        </Router>
    }
}


// 页面内容
export const Page = ({match}) =>{
    const data = getPageData(match.params.id, match.params.subid);
    return parseData(data);
}
相关推荐
小豆包api1 天前
小豆包AI API × Nano Banana:3D手办 + AI视频生成,「动起来」的神级玩法!
前端·api
布列瑟农的星空1 天前
大话设计模式——观察者模式和发布/订阅模式的区别
前端·后端·架构
龙在天1 天前
Vue3 实现 B站 视差 动画
前端
KenXu1 天前
F2C Prompt to Design、AI 驱动的设计革命
前端
小鱼儿亮亮1 天前
canvas中画线条,线条效果比预期宽1像素且模糊问题分析及解决方案
前端·react.js
@大迁世界1 天前
用 popover=“hint“ 打造友好的 HTML 提示:一招让界面更“懂人”
开发语言·前端·javascript·css·html
伍哥的传说1 天前
Tailwind CSS v4 终极指南:体验 Rust 驱动的闪电般性能与现代化 CSS 工作流
前端·css·rust·tailwindcss·tailwind css v4·lightning css·utility-first
小鱼儿亮亮1 天前
使用Redux的combineReducers对数据拆分
前端·react.js
定栓1 天前
Typescript入门-类型断言讲解
前端·javascript·typescript
码间舞1 天前
你不知道的pnpm!如果我的电脑上安装了nvm,切换node版本后,那么pnpm还会共享一个磁盘的npm包吗?
前端·代码规范·前端工程化