Ashun's 技術駅 Ashun's 技術駅
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • Vue
  • 现代web布局
  • React
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 技术资源
  • 第一阶段

    • HTML
  • 第二阶段

    • JavaScript
  • 第三阶段

    • Vue
  • 第四阶段

    • 实战项目
  • 每周测试

    • 每周
  • 其他

    • Vue引入UI框架
    • Web前端面试
    • Vue3-resource
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 福利资源
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Ashun

前端界的小学生
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • Vue
  • 现代web布局
  • React
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 技术资源
  • 第一阶段

    • HTML
  • 第二阶段

    • JavaScript
  • 第三阶段

    • Vue
  • 第四阶段

    • 实战项目
  • 每周测试

    • 每周
  • 其他

    • Vue引入UI框架
    • Web前端面试
    • Vue3-resource
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 福利资源
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • vue

  • vue3

  • es6

  • JavaScript

  • css

  • webpack

  • http

  • NodeJS

  • React

    • 01React
    • 02Real DOM_Virtual DOM
    • 03life cycle
    • 04state_props
    • 05super()_super(props)
    • 06setState
    • 07SyntheticEvent
    • 08Binding events
    • 09Building components
    • 10communication
    • 11key
    • 12React refs
    • 13class_function component
    • 14controlled_Uncontrolled
    • 15High order components
    • 16React Hooks
    • 17import css
    • 18animation
    • 19redux
    • 20Redux Middleware
    • 21how to use redux
    • 22React Router
      • 面试官:说说你对React Router的理解?常用的Router组件有哪些?
      • 一、是什么
      • 二、有哪些
        • BrowserRouter、HashRouter
        • Route
        • Link、NavLink
        • redirect
        • switch
        • useHistory
        • useParams
        • useLocation
      • 三、参数传递
        • 动态路由
        • search传递参数
        • to传入对象
      • 参考文献
    • 23React Router model
    • 24immutable
    • 25render
    • 26improve_render
    • 27diff
    • 28Fiber
    • 29JSX to DOM
    • 30Improve performance
    • 31capture error
    • 32server side rendering
    • 33summary
  • git

  • linux

  • typescript

  • algorithm

  • applet

  • design

  • 《Web前端面试》
  • React
xugaoyi
2022-03-25
目录

22React Router

# 面试官:说说你对React Router的理解?常用的Router组件有哪些?

# 一、是什么

react-router等前端路由的原理大致相同,可以实现无刷新的条件下切换显示不同的页面

路由的本质就是页面的URL发生改变时,页面的显示结果可以根据URL的变化而变化,但是页面不会刷新

因此,可以通过前端路由可以实现单页(SPA)应用

react-router主要分成了几个不同的包:

  • react-router: 实现了路由的核心功能

  • react-router-dom: 基于 react-router,加入了在浏览器运行环境下的一些功能

  • react-router-native:基于 react-router,加入了 react-native 运行环境下的一些功能

  • react-router-config: 用于配置静态路由的工具库

# 二、有哪些

这里主要讲述的是react-router-dom的常用API,主要是提供了一些组件:

  • BrowserRouter、HashRouter
  • Route
  • Link、NavLink
  • switch
  • redirect

# BrowserRouter、HashRouter

Router中包含了对路径改变的监听,并且会将相应的路径传递给子组件

BrowserRouter是history模式,HashRouter模式

使用两者作为最顶层组件包裹其他组件

import { BrowserRouter as Router } from "react-router-dom";

export default function App() {
  return (
    <Router>
      <main>
        <nav>
          <ul>
            <li>
              < a href=" ">Home</ a>
            </li>
            <li>
              < a href="/about">About</ a>
            </li>
            <li>
              < a href="/contact">Contact</ a>
            </li>
          </ul>
        </nav>
      </main>
    </Router>
  );
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# Route

Route用于路径的匹配,然后进行组件的渲染,对应的属性如下:

  • path 属性:用于设置匹配到的路径
  • component 属性:设置匹配到路径后,渲染的组件
  • render 属性:设置匹配到路径后,渲染的内容
  • exact 属性:开启精准匹配,只有精准匹配到完全一致的路径,才会渲染对应的组件
import { BrowserRouter as Router, Route } from "react-router-dom";

export default function App() {
  return (
    <Router>
      <main>
        <nav>
          <ul>
            <li>
              < a href="/">Home</ a>
            </li>
            <li>
              < a href="/about">About</ a>
            </li>
            <li>
              < a href="/contact">Contact</ a>
            </li>
          </ul>
        </nav>
        <Route path="/" render={() => <h1>Welcome!</h1>} />
      </main>
    </Router>
  );
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# Link、NavLink

通常路径的跳转是使用Link组件,最终会被渲染成a元素,其中属性to代替a标题的href属性

NavLink是在Link基础之上增加了一些样式属性,例如组件被选中时,发生样式变化,则可以设置NavLink的一下属性:

  • activeStyle:活跃时(匹配时)的样式
  • activeClassName:活跃时添加的class

如下:

<NavLink to="/" exact activeStyle={{color: "red"}}>首页</NavLink>
<NavLink to="/about" activeStyle={{color: "red"}}>关于</NavLink>
<NavLink to="/profile" activeStyle={{color: "red"}}>我的</NavLink>
1
2
3

如果需要实现js实现页面的跳转,那么可以通过下面的形式:

通过Route作为顶层组件包裹其他组件后,页面组件就可以接收到一些路由相关的东西,比如props.history

const Contact = ({ history }) => (
  <Fragment>
    <h1>Contact</h1>
    <button onClick={() => history.push("/")}>Go to home</button>
    <FakeText />
  </Fragment>
);
1
2
3
4
5
6
7

props中接收到的history对象具有一些方便的方法,如goBack,goForward,push

# redirect

用于路由的重定向,当这个组件出现时,就会执行跳转到对应的to路径中,如下例子:

const About = ({
  match: {
    params: { name },
  },
}) => (
  // props.match.params.name
  <Fragment>
    {name !== "tom" ? <Redirect to="/" /> : null}
    <h1>About {name}</h1>
    <FakeText />
  </Fragment>
)
1
2
3
4
5
6
7
8
9
10
11
12

上述组件当接收到的路由参数name 不等于 tom 的时候,将会自动重定向到首页

# switch

swich组件的作用适用于当匹配到第一个组件的时候,后面的组件就不应该继续匹配

如下例子:

<Switch>
  <Route exact path="/" component={Home} />
  <Route path="/about" component={About} />
  <Route path="/profile" component={Profile} />
  <Route path="/:userid" component={User} />
  <Route component={NoMatch} />
</Switch>
1
2
3
4
5
6
7

如果不使用switch组件进行包裹

除了一些路由相关的组件之外,react-router还提供一些hooks,如下:

  • useHistory
  • useParams
  • useLocation

# useHistory

useHistory可以让组件内部直接访问history,无须通过props获取

import { useHistory } from "react-router-dom";

const Contact = () => {
  const history = useHistory();
  return (
    <Fragment>
      <h1>Contact</h1>
      <button onClick={() => history.push("/")}>Go to home</button>
    </Fragment>
  );
};
1
2
3
4
5
6
7
8
9
10
11

# useParams

const About = () => {
  const { name } = useParams();
  return (
    // props.match.params.name
    <Fragment>
      {name !== "John Doe" ? <Redirect to="/" /> : null}
      <h1>About {name}</h1>
      <Route component={Contact} />
    </Fragment>
  );
};
1
2
3
4
5
6
7
8
9
10
11

# useLocation

useLocation 会返回当前 URL的 location对象

import { useLocation } from "react-router-dom";

const Contact = () => {
  const { pathname } = useLocation();

  return (
    <Fragment>
      <h1>Contact</h1>
      <p>Current URL: {pathname}</p >
    </Fragment>
  );
};
1
2
3
4
5
6
7
8
9
10
11
12

# 三、参数传递

这些路由传递参数主要分成了三种形式:

  • 动态路由的方式
  • search传递参数
  • to传入对象

# 动态路由

动态路由的概念指的是路由中的路径并不会固定

例如将path在Route匹配时写成/detail/:id,那么 /detail/abc、/detail/123都可以匹配到该Route

<NavLink to="/detail/abc123">详情</NavLink>

<Switch>
    ... 其他Route
    <Route path="/detail/:id" component={Detail}/>
    <Route component={NoMatch} />
</Switch>
1
2
3
4
5
6
7

获取参数方式如下:

console.log(props.match.params.xxx)
1

# search传递参数

在跳转的路径中添加了一些query参数;

<NavLink to="/detail2?name=why&age=18">详情2</NavLink>

<Switch>
  <Route path="/detail2" component={Detail2}/>
</Switch>
1
2
3
4
5

获取形式如下:

console.log(props.location.search)
1

# to传入对象

传递方式如下:

<NavLink to={{
    pathname: "/detail2", 
    query: {name: "kobe", age: 30},
    state: {height: 1.98, address: "洛杉矶"},
    search: "?apikey=123"
  }}>
  详情2
</NavLink>
1
2
3
4
5
6
7
8

获取参数的形式如下:

console.log(props.location)
1

# 参考文献

  • http://react-guide.github.io/react-router-cn/docs/API.html#route (opens new window)
编辑 (opens new window)
上次更新: 2023/08/06, 00:38:41
21how to use redux
23React Router model

← 21how to use redux 23React Router model→

最近更新
01
课件-react路由-V6
01-22
02
课件-国际化
01-22
03
课件-redux-toolkit
01-22
更多文章>
Theme by Vdoing | Copyright © 2019-2024 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式