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

长沙百度网站建设蜜雪冰城推广软文

长沙百度网站建设,蜜雪冰城推广软文,淘客网站怎么做排名,目前流行的网站开发工具手写一个简化版本的 React Router,主要实现其核心功能,包括路由配置、路径匹配和导航。 这个简化版本将包括以下组件和函数: 1. BrowserRouter 2. Routes 和 Route 3. Link 4. useRoutes 5. useParams 1. 基本框架和组件 1.1. Browse…

手写一个简化版本的 React Router,主要实现其核心功能,包括路由配置、路径匹配和导航。

这个简化版本将包括以下组件和函数:

1. BrowserRouter

2. Routes 和 Route

3. Link

4. useRoutes

5. useParams

1. 基本框架和组件

1.1. BrowserRouter

BrowserRouter 组件使用 HTML5 History API,并为应用提供路由上下文。

import React, { useState, useEffect, createContext, useContext } from 'react';const RouterContext = createContext();function BrowserRouter({ children }) {const [location, setLocation] = useState(window.location.pathname);useEffect(() => {const handlePopState = () => setLocation(window.location.pathname);window.addEventListener('popstate', handlePopState);return () => window.removeEventListener('popstate', handlePopState);}, []);const navigate = (to) => {window.history.pushState({}, '', to);setLocation(to);};return (<RouterContext.Provider value={{ location, navigate }}>{children}</RouterContext.Provider>);
}

1.2. Routes 和 Route

Routes 组件遍历所有子路由,并匹配当前路径,Route 组件定义路径和对应的组件。

function Routes({ children }) {const { location } = useContext(RouterContext);let element;React.Children.forEach(children, (child) => {if (!element && React.isValidElement(child) && matchPath(child.props.path, location)) {element = child;}});return element;
}function Route({ path, element }) {return element;
}

1.3. Link

Link 组件用于导航。

function Link({ to, children }) {const { navigate } = useContext(RouterContext);const handleClick = (event) => {event.preventDefault();navigate(to);};return <a href={to} onClick={handleClick}>{children}</a>;
}

1.4. useRoutes

useRoutes 是一个 hook,用于在函数组件中使用路由。

function useRoutes(routes) {const { location } = useContext(RouterContext);for (let route of routes) {if (matchPath(route.path, location)) {return route.element;}}return null;
}

1.5. useParams

useParams 是一个 hook,用于获取路径参数。

function useParams() {const { location } = useContext(RouterContext);const path = location;const match = path.match(/\/([^\/]+)\/?/);return match ? { id: match[1] } : {};
}

2. 路径匹配函数

matchPath 用于检查路径是否匹配。

function matchPath(pattern, pathname) {const regex = new RegExp(`^${pattern.replace(/:\w+/g, '([^/]+)')}$`);return regex.test(pathname);
}

3. 示例应用

基础定义完成,我们在 React 中使用一下这个 router 。

import React from 'react';
import ReactDOM from 'react-dom';function Home() {return <h2>Home</h2>;
}function About() {return <h2>About</h2>;
}function User() {const { id } = useParams();return <h2>User ID: {id}</h2>;
}function App() {return (<BrowserRouter><nav><Link to="/">Home</Link><Link to="/about">About</Link><Link to="/user/1">User 1</Link></nav><Routes><Route path="/" element={<Home />} /><Route path="/about" element={<About />} /><Route path="/user/:id" element={<User />} /></Routes></BrowserRouter>);
}ReactDOM.render(<App />, document.getElementById('root'));

这个简化版的 React Router,主要实现其核心功能,包括路由配置、路径匹配和导航。省略了很多边缘情况和优化,从本例中可以窥见 react-router 框架整体实现最核心的思路。

4. 补充资料

  • 官方文档:React Router Official Documentation

  • react-router-dom 多类型历史:https://github.com/remix-run/react-router/blob/acc2b94088835d1247bdf3a3f883f74cc3570a0/packages/react-router-dom/index.tsx#L262

  • wouter: https://github.com/molefrog/wouter

  • @tanstack/router:TanStack Router

  • 浏览器历史记录协议:History API - Web APIs | MDN

http://www.hrbkazy.com/news/12388.html

相关文章:

  • 如何做白日梦的网站it培训机构
  • 无锡找做网站百度商家平台
  • 临沂网站建设goldball免费的seo
  • 江门网站制作推广站长之家产品介绍
  • 手机网站制作平台有哪些百度交易平台官网
  • 在浴室里做的网站桂林seo顾问
  • axure网站设计案例无锡网站推广公司
  • 学校网站班级网页建设制度seo推广工具
  • 济南网站建设培训学校自己如何制作一个小程序
  • 网站建设亿金手指花总12十大营销策略有哪些
  • 网站公司销售百度seo关键词排名优化工具
  • 网站做不做百度云加速谷粉搜索谷歌搜索
  • 网站鼠标悬停动态效果上海关键词排名优化价格
  • 辽宁建网站爱站网长尾关键词挖掘工具的作用
  • 手机网站免费做app济南优化seo公司
  • 哪个网站可以做图片链接郑州网站seo推广
  • 南联网站建设推广惠州网站关键词排名
  • 建设农家书屋官方网站管理人员课程培训
  • 吉林市做网站公司国外常用的seo站长工具
  • 国外设计网站door网络营销策略有哪五种
  • b2c网站建设需要的条件看广告赚钱
  • 柳市做网站建设谷歌chrome官网
  • 漯河网站建设费用真人seo点击平台
  • 泉州最专业微信网站建设开发百度搜索词排名
  • 摄影设计seo的优化流程
  • 现在c 做网站用什么软件seo搜索引擎推广什么意思
  • 网站被挂黑后果镇江网站定制
  • 俄罗斯网站开发惠州seo外包费用
  • php制作网站拼多多关键词排名在哪里看
  • 网站规划和建设方案b站视频怎么快速推广