当前位置: 首页 > news >正文

React路由快速入门:Class组件和函数式组件的使用

1. 介绍

在这里插入图片描述
在开始学习React路由之前,先了解一下什么是React路由。React Router是一个为React应用程序提供声明式路由的库。它可以帮助您在应用程序中管理不同的URL,并在这些URL上呈现相应的组件。

2. 安装

要在React应用程序中使用React路由,您需要安装以下两个包:

  • react-router-dom
  • react-router

您可以使用npm或yarn将其添加到项目中:

npm install react-router-dom react-router
# or
yarn add react-router-dom react-router

3. 使用Class组件

在使用React路由之前,首先要导入所需的组件:

import { BrowserRouter as Router, Route, Link, Switch } from 'react-router-dom';

接下来,我们将创建一些基本的组件,以展示如何使用React路由:

import React, { Component } from 'react';class Home extends Component {render() {return (<div><h1>Home</h1></div>);}
}class About extends Component {render() {return (<div><h1>About</h1></div>);}
}class Contact extends Component {render() {return (<div><h1>Contact</h1></div>);}
}

现在,我们将在主应用程序组件中设置路由:

class App extends Component {render() {return (<Router><div><nav><ul><li><Link to="/">Home</Link></li><li><Link to="/about">About</Link></li><li><Link to="/contact">Contact</Link></li></ul></nav><Switch><Route exact path="/" component={Home} /><Route path="/about" component={About} /><Route path="/contact" component={Contact} /></Switch></div></Router>);}
}

在上面的示例中,我们使用BrowserRouter组件在应用程序中创建了一个新路由。Link组件用于在应用程序中导航,而无需刷新页面。Route组件用于在特定URL上呈现组件。Switch组件确保仅渲染与当前URL匹配的第一个Route

4. 使用函数式组件

在使用React路由与函数式组件时,首先要导入所需的组件和钩子:

import { BrowserRouter as Router, Route, Link, Switch, useRouteMatch, useParams } from 'react-router-dom';

接下来,我们将创建一些基本的函数式组件:

const Home = () => {return (<div><h1>Home</h1></div>);
};const About = () => {return (<div><h1>About</h1></div>);
};const Contact = () => {return (<div><h1>Contact</h1></div>);
};

现在,我们将在主应用程序组件中设置路由:

const App = () => {return (<Router><div><nav><ul><li><Link to="/">Home</Link></li><li><Link to="/about">About</Link></li><li><Link to="/contact">Contact</Link></li></ul></nav><Switch><Route exact path="/" component={Home} /><Route path="/about" component={About} /><Route path="/contact" component={Contact} /></Switch></div></Router>);
};

在上面的示例中,我们使用函数式组件实现了与Class组件相同的功能。

5. 总结

我们探讨了如何使用Class组件和函数式组件来设置路由。通过掌握React路由的基础知识,您可以轻松地为自己的React应用程序创建复杂的导航系统。更多的学习资料可参考:

React Router 中文文档:https://react-guide.github.io/react-router-cn/index.html
React Router 教程: https://www.freecodecamp.org/news/react-router-in-5-minutes/
React Router官方文档: https://reactrouter.com/

http://www.lryc.cn/news/335255.html

相关文章:

  • Pytorch数据结构:GPU加速
  • OpenHarmony开发-连接开发板调试应用
  • RabbitMQ如何保证消息的幂等性???
  • 【QT】Qt Charts的实际使用中的一些小细节完善如:resetZoom、fitInView
  • 上位机图像处理和嵌入式模块部署(qmacvisual缺失的颜色检测)
  • macOS制作C/C++ app
  • 学习笔记——C语言基本概念链表——(12)
  • 开发环境解决跨域问题
  • 物理服务器与云服务器的租用对比
  • Ubuntu Desktop Server - Resource temporarily unavailable
  • 用Wireshark工具对gRPC接口进行本地抓包
  • 科技动态人工智能应用太空探索生物科技
  • 如何让MacOS「终端」走代理
  • 数码相框-显示JPG图片
  • PHP自带的密码加密函数Password_verify 和password_hash
  • UVA1596 Bug Hunt 找Bug 解题报告
  • Java编程题 | 提取整数的特定位数
  • 设置你的第一个React应用
  • 【黑马头条】-day07APP端文章搜索-ES-mongoDB
  • SSL数字证书
  • 番茄 abogus rpc调用
  • CSS设置元素的宽高比
  • jenkins+docker实现可持续自动化部署springboot项目
  • 【LAMMPS学习】八、基本知识的讨论(1.8)键的断裂
  • GPT提示词分享 —— 中医
  • 什么的零日攻击,如何防御零日攻击
  • MySQL 建表语句详解
  • 【Linux】虚拟化技术docker搭建SuitoCRM系统及汉化
  • P8707 [蓝桥杯 2020 省 AB1] 走方格
  • Window安装PostgresSQL