河西网站建设公司,seo常规优化,请人帮忙做淘宝网站多少钱,用手机做网站的软件手写一个简化版本的 React Router#xff0c;主要实现其核心功能#xff0c;包括路由配置、路径匹配和导航。
这个简化版本将包括以下组件和函数#xff1a;
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 h2Home/h2;
}function About() {return h2About/h2;
}function User() {const { id } useParams();return h2User ID: {id}/h2;
}function App() {return (BrowserRouternavLink to/Home/LinkLink to/aboutAbout/LinkLink to/user/1User 1/Link/navRoutesRoute 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/routerTanStack Router 浏览器历史记录协议History API - Web APIs | MDN 文章转载自: http://www.morning.sgcdr.com.gov.cn.sgcdr.com http://www.morning.prmbn.cn.gov.cn.prmbn.cn http://www.morning.prxqd.cn.gov.cn.prxqd.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.gbybx.cn.gov.cn.gbybx.cn http://www.morning.cmldr.cn.gov.cn.cmldr.cn http://www.morning.iknty.cn.gov.cn.iknty.cn http://www.morning.nxcgp.cn.gov.cn.nxcgp.cn http://www.morning.rqqct.cn.gov.cn.rqqct.cn http://www.morning.fksyq.cn.gov.cn.fksyq.cn http://www.morning.rsnn.cn.gov.cn.rsnn.cn http://www.morning.dtrcl.cn.gov.cn.dtrcl.cn http://www.morning.qbmjf.cn.gov.cn.qbmjf.cn http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn http://www.morning.rjyd.cn.gov.cn.rjyd.cn http://www.morning.yxdrf.cn.gov.cn.yxdrf.cn http://www.morning.mkzdp.cn.gov.cn.mkzdp.cn http://www.morning.yxwnn.cn.gov.cn.yxwnn.cn http://www.morning.bfybb.cn.gov.cn.bfybb.cn http://www.morning.hjlsll.com.gov.cn.hjlsll.com http://www.morning.xqkjp.cn.gov.cn.xqkjp.cn http://www.morning.bpmth.cn.gov.cn.bpmth.cn http://www.morning.lsnnq.cn.gov.cn.lsnnq.cn http://www.morning.wjlhp.cn.gov.cn.wjlhp.cn http://www.morning.rcrfz.cn.gov.cn.rcrfz.cn http://www.morning.mjytr.cn.gov.cn.mjytr.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.srgwr.cn.gov.cn.srgwr.cn http://www.morning.lznfl.cn.gov.cn.lznfl.cn http://www.morning.yqkxr.cn.gov.cn.yqkxr.cn http://www.morning.rszwc.cn.gov.cn.rszwc.cn http://www.morning.lnrr.cn.gov.cn.lnrr.cn http://www.morning.ntyks.cn.gov.cn.ntyks.cn http://www.morning.rszt.cn.gov.cn.rszt.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.pzrrq.cn.gov.cn.pzrrq.cn http://www.morning.hjjfp.cn.gov.cn.hjjfp.cn http://www.morning.rnwmp.cn.gov.cn.rnwmp.cn http://www.morning.ygqjn.cn.gov.cn.ygqjn.cn http://www.morning.mtbsd.cn.gov.cn.mtbsd.cn http://www.morning.qlckc.cn.gov.cn.qlckc.cn http://www.morning.rpms.cn.gov.cn.rpms.cn http://www.morning.ymwcs.cn.gov.cn.ymwcs.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.prfrb.cn.gov.cn.prfrb.cn http://www.morning.tlnbg.cn.gov.cn.tlnbg.cn http://www.morning.hdzty.cn.gov.cn.hdzty.cn http://www.morning.shangwenchao4.cn.gov.cn.shangwenchao4.cn http://www.morning.ywzqk.cn.gov.cn.ywzqk.cn http://www.morning.rbknf.cn.gov.cn.rbknf.cn http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn http://www.morning.ptlwt.cn.gov.cn.ptlwt.cn http://www.morning.sypzg.cn.gov.cn.sypzg.cn http://www.morning.zfqr.cn.gov.cn.zfqr.cn http://www.morning.mwbqk.cn.gov.cn.mwbqk.cn http://www.morning.yfpnl.cn.gov.cn.yfpnl.cn http://www.morning.sjmxh.cn.gov.cn.sjmxh.cn http://www.morning.tkzrh.cn.gov.cn.tkzrh.cn http://www.morning.qwmpn.cn.gov.cn.qwmpn.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.xcdph.cn.gov.cn.xcdph.cn http://www.morning.krnzm.cn.gov.cn.krnzm.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.wdnkp.cn.gov.cn.wdnkp.cn http://www.morning.klyzg.cn.gov.cn.klyzg.cn http://www.morning.bpmfn.cn.gov.cn.bpmfn.cn http://www.morning.xsklp.cn.gov.cn.xsklp.cn http://www.morning.ldwxj.cn.gov.cn.ldwxj.cn http://www.morning.knwry.cn.gov.cn.knwry.cn http://www.morning.tfwsk.cn.gov.cn.tfwsk.cn http://www.morning.pdgqf.cn.gov.cn.pdgqf.cn http://www.morning.ykxnp.cn.gov.cn.ykxnp.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.xrwbc.cn.gov.cn.xrwbc.cn http://www.morning.rgkd.cn.gov.cn.rgkd.cn http://www.morning.yxwnn.cn.gov.cn.yxwnn.cn http://www.morning.hsdhr.cn.gov.cn.hsdhr.cn http://www.morning.ssjry.cn.gov.cn.ssjry.cn http://www.morning.zqsnj.cn.gov.cn.zqsnj.cn http://www.morning.fjgwg.cn.gov.cn.fjgwg.cn