亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > JS > 正文

淺談react-router@4.0 使用方法和源碼分析

2024-05-06 16:51:47
字體:
來源:轉載
供稿:網友

react-router-dom@4.3.0 || react-router@4.4.1

react-router 使用方法

配置 router.js

import React, { Component } from 'react';import { Switch, Route } from 'react-router-dom';const router = [{  path: '/',  exact: true,  component:importPath({   loader: () => import(/* webpackChunkName:"home" */ "pages/home/index.js"),  }), },]const Routers = () => ( <main>  <Switch>   {    router.map(({component,path,exact},index)=>{     return <Route exact={exact} path={path} component={component} key={path} />    })   }  </Switch> </main>);export default Routers;

入口 index.js

import {HashRouter} from 'react-router-dom';import React from 'react';import ReactDOM from 'react-dom';import Routers from './router';ReactDOM.render (   <HashRouter>    <Routers />   </HashRouter>, document.getElementById ('App'));

home.js

import { withRouter } from "react-router-dom";@withRouterclass Home extends React.Component<PropsType, stateType> { constructor(props: PropsType) {  super(props);  this.state = {}; } goPath=()=>{   this.props.history.push('/home') } render() {  return (   <div onClick={this.goPath}>home</div>  ); }export default Home;

react-router 源碼解析

下面代碼中會移除部分的類型檢查和提醒代碼,突出重點代碼

第一步 Switch react-router

function _possibleConstructorReturn(self, call) { if (!self) {  throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } if(call&&(typeof call === "object" || typeof call === "function") ){  return call }else {  return self }}var Switch = function (_React$Component) { function Switch() {  //使用傳遞進來的組件覆蓋本身  return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));  } Switch.prototype.render = function render() {  var route = this.context.router.route;  var children = this.props.children;  var location = this.props.location || route.location;  var match = void 0,child = void 0;    //檢查element是否是react組件,初始match為null,  React.Children.forEach(children, function (element) {   //如果match符合,forEach不會進入該if   if (match == null && React.isValidElement(element)) {     var _element$props = element.props,      pathProp = _element$props.path,      exact = _element$props.exact,      strict = _element$props.strict,      sensitive = _element$props.sensitive,      from = _element$props.from;    var path = pathProp || from;    child = element;     //檢查當前配置是否符合,    match = matchPath(location.pathname, { path: path, exact: exact, strict: strict, sensitive: sensitive }, route.match);    }  });  //如果有匹配元素,則返回克隆child  return match ? React.cloneElement(child, { location: location, computedMatch: match }) : null; }; return Switch;}(React.Component);

總結:switch根據location.pathname,path,exact,strict,sensitive獲取元素并返回element

第二步 Route react-router

var Route = function (_React$Component) { function Route() {  var _temp, _this, _ret;  //獲取參數  for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {   args[_key] = arguments[_key];  }  //修改this  return _ret = (   _temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this),    //檢查當前元素是否符合match   _this.state = {match: _this.computeMatch(_this.props,_this.context.router)},_temp),    //這里是真正return    _possibleConstructorReturn(_this, _ret);  } // 設置content Route.prototype.getChildContext = function getChildContext() {  return {   router: _extends({}, this.context.router, {    route: {     location: this.props.location || this.context.router.route.location,     match: this.state.match    }   })  }; }; // 根據參數檢查當前元素是否符合匹配規則 Route.prototype.computeMatch = function computeMatch(_ref, router) {  var computedMatch = _ref.computedMatch,    location = _ref.location,    path = _ref.path,    strict = _ref.strict,    exact = _ref.exact,    sensitive = _ref.sensitive;  if (computedMatch) return computedMatch;  var route = router.route;  var pathname = (location || route.location).pathname;  return matchPath(pathname, { path: path, strict: strict, exact: exact, sensitive: sensitive }, route.match); }; // 設置match Route.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps, nextContext) {  this.setState({   match: this.computeMatch(nextProps, nextContext.router)  }); }; Route.prototype.render = function render() {  var match = this.state.match;  var _props = this.props,    children = _props.children,    component = _props.component,    render = _props.render;  var _context$router = this.context.router,    history = _context$router.history,    route = _context$router.route,    staticContext = _context$router.staticContext;  var location = this.props.location || route.location;  var props = { match: match, location: location, history: history, staticContext: staticContext };  //檢查route 是否有component組  if (component) return match ? React.createElement(component, props) : null;   // 檢查是否包含render 組件  if (render) return match ? render(props) : null;  // withRouter 使用的方式  if (typeof children === "function") return children(props);  if (children && !isEmptyChildren(children)) return React.Children.only(children);  return null; }; return Route;}(React.Component);

總結:route 渲染的方式: component render children,代碼示例用的是component,route 是檢查當前組件是否符合路由匹配規則并執行創建過程

第三步 HashRouter react-router-dom

import Router from './Router'import {createHistory} from 'history'var HashRouter = function (_React$Component) { function HashRouter() {  var _temp, _this, _ret;  //參數轉換為數組  for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {    args[_key] = arguments[_key];  }  return _ret = (   _temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this),    _this.history = createHistory(_this.props), _temp), //創建history    _possibleConstructorReturn(_this, _ret); //真正返回的東西 返回this } HashRouter.prototype.render = function render() {  // 返回一個Router,并且把history,children傳遞給Router  return React.createElement(Router, { history: this.history, children: this.props.children }); }; return HashRouter;}(React.Component);

總結 通過 history庫里面 createHistory 創建路由系統

第四部 Router react-router

var Router = function (_React$Component) { function Router() {  var _temp, _this, _ret;  //獲取參數,和其他組件一樣  for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {   args[_key] = arguments[_key];  }  return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = {   match: _this.computeMatch(_this.props.history.location.pathname) //返回路由對象  }, _temp), _possibleConstructorReturn(_this, _ret); //返回this } // 返回context Router.prototype.getChildContext = function getChildContext() {  return {   router: _extends({}, this.context.router, {    history: this.props.history,    route: {     location: this.props.history.location,     match: this.state.match    }   })  }; };   Router.prototype.computeMatch = function computeMatch(pathname) {  return {   path: "/",   url: "/",   params: {},   isExact: pathname === "/"  }; }; Router.prototype.componentWillMount = function componentWillMount() {  var _this2 = this;  var _props = this.props,    children = _props.children,    history = _props.history;  // 啟動監聽 當hash 改變是做一次檢查,并返回unlisten 取消事件  this.unlisten = history.listen(function () {   _this2.setState({    match: _this2.computeMatch(history.location.pathname)   });  }); }; //銷毀前取消監聽 Router.prototype.componentWillUnmount = function componentWillUnmount() {  this.unlisten(); }; // children是HashRouter 傳遞進來的 Router.prototype.render = function render() {  var children = this.props.children;  return children ? React.Children.only(children) : null; }; return Router;}(React.Component);

總結 history是一個JavaScript庫,可讓您在JavaScript運行的任何地方輕松管理會話歷史記錄。history抽象出各種環境中的差異,并提供最小的API,使您可以管理歷史堆棧,導航,確認導航以及在會話之間保持狀態。

第五部 withRouter <react-router>

var withRouter = function withRouter(Component) { var C = function C(props) {  //獲取props  var wrappedComponentRef = props.wrappedComponentRef,    remainingProps = _objectWithoutProperties(props, ["wrappedComponentRef"]);  // Route 組件 children方式  return React.createElement(Route, {   children: function children(routeComponentProps) {    // 這里使用的是route 組件 children(props)    //routeComponentProps 實際等于 { match: match, location: location, history: history, staticContext: staticContext };    return React.createElement(Component, _extends({}, remainingProps, routeComponentProps, {     ref: wrappedComponentRef    }));   }  }); }; C.displayName = "withRouter(" + (Component.displayName || Component.name) + ")"; C.WrappedComponent = Component; // 該類似于object.assign(C,Component),得到的結果是C return hoistStatics(C, Component);};

到這里真個流程基本結束了,這只是react-router的一種使用方式的解析,本文的目的是理解react-router的運行機制,如果有什么錯誤還望指出,謝謝

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产mv免费观看入口亚洲| 国产成人精品久久| 国产午夜精品理论片a级探花| 久久亚洲精品国产亚洲老地址| 国产精品扒开腿做爽爽爽视频| 91热精品视频| 亚洲精品美女免费| 亚洲欧美日韩精品久久亚洲区| 精品呦交小u女在线| 成人黄色免费在线观看| 亚洲视频免费一区| 亚洲国产精品va在线| 国产精品久久久久久久一区探花| 成人网址在线观看| 亚洲精品视频免费| 成人黄色生活片| 欧美成人免费全部观看天天性色| 日韩一区二区欧美| 国内精品模特av私拍在线观看| 欧美影院成年免费版| 日本电影亚洲天堂| 91久久国产婷婷一区二区| 精品国产一区二区三区久久久狼| 亚洲人成网站777色婷婷| 亚洲有声小说3d| 草民午夜欧美限制a级福利片| 欧美一区二区三区艳史| 日韩在线免费视频| 久久精品国产成人| 国产成人久久久| 亚洲深夜福利网站| 欧美性猛交xxxx富婆弯腰| 国产狼人综合免费视频| 色多多国产成人永久免费网站| 国产精品视频最多的网站| 国产成+人+综合+亚洲欧洲| 中文字幕亚洲二区| 日韩经典一区二区三区| 欧美电影第一页| 亚洲美女自拍视频| 91精品在线播放| 日韩欧美999| 69视频在线播放| 国自在线精品视频| 最新日韩中文字幕| 欧美激情网站在线观看| 精品欧美激情精品一区| 成人免费福利在线| 亚洲国产高潮在线观看| 亚洲精品福利免费在线观看| 国产精品极品美女粉嫩高清在线| 国产精品成久久久久三级| 亚洲天堂久久av| 日韩福利视频在线观看| 欧美成人一区在线| 中文在线资源观看视频网站免费不卡| 国产成人av网| 欧美成人免费全部观看天天性色| 国产精品视频久| 91九色视频在线| 中文字幕在线观看亚洲| 国产精品欧美亚洲777777| 欧洲s码亚洲m码精品一区| 午夜精品免费视频| 精品久久久久久国产91| 国产精品国语对白| 亚洲视频在线免费观看| 日本不卡免费高清视频| 色悠久久久久综合先锋影音下载| 日韩精品福利网站| 成人免费高清完整版在线观看| 九九久久久久久久久激情| 亚洲欧洲日本专区| 欧美午夜精品在线| 国产性猛交xxxx免费看久久| 不卡av电影在线观看| 久久久久久久影院| 亚洲电影免费在线观看| 欧美高清第一页| 久久躁日日躁aaaaxxxx| 亚洲成人教育av| 在线午夜精品自拍| 亚洲韩国青草视频| 成人淫片在线看| 91精品一区二区| 少妇高潮 亚洲精品| 久久亚洲成人精品| 日韩中文字幕国产精品| 日韩中文字幕第一页| 91成人在线观看国产| 成人乱人伦精品视频在线观看| 日韩中文字幕在线| 国产精品永久免费视频| 欧美激情欧美激情在线五月| 亚洲人午夜色婷婷| 久久伊人精品一区二区三区| 中文字幕国产精品久久| 欧美一级电影久久| 久久精品久久久久电影| 成人性生交xxxxx网站| 最近中文字幕mv在线一区二区三区四区| 国产精品福利久久久| 国产精品美女在线观看| 欧美黄网免费在线观看| 亚洲跨种族黑人xxx| 日韩在线中文字幕| 日日噜噜噜夜夜爽亚洲精品| 中文字幕亚洲一区在线观看| 国产精品成人av在线| 欧美激情啊啊啊| 日韩中文字幕精品| 久久视频这里只有精品| 亚洲最大成人网色| 国产亚洲欧美另类中文| 亚洲第一男人天堂| 国产亚洲成精品久久| 日韩经典一区二区三区| 日韩欧美精品网站| 国产91亚洲精品| 成人欧美一区二区三区黑人| 日韩欧美视频一区二区三区| 深夜福利一区二区| 国产丝袜精品第一页| 亚洲午夜性刺激影院| 国产精品永久免费| 精品成人在线视频| 久久九九有精品国产23| 国产亚洲欧美一区| 国产视频福利一区| 欧美一级片免费在线| 在线看福利67194| 成人av.网址在线网站| 久久久女人电视剧免费播放下载| 97在线视频精品| 日韩专区在线观看| 欧美日韩第一视频| 国产成人jvid在线播放| 日韩大胆人体377p| 日韩欧美在线免费观看| 国产99久久精品一区二区 夜夜躁日日躁| 日韩免费不卡av| 88国产精品欧美一区二区三区| 国产视频福利一区| 国产成人欧美在线观看| 久久精品小视频| 精品福利免费观看| 欧美最猛性xxxx| 久久av在线看| 日韩免费视频在线观看| 国产精品高清在线观看| 国产精品成av人在线视午夜片| 亚洲天堂av在线播放| 91在线观看免费高清| 日韩中文字幕在线视频| 亚洲天堂视频在线观看| 亚洲成人精品av| 中文字幕一精品亚洲无线一区| 国产精品美女久久久免费| 草民午夜欧美限制a级福利片| 国产精品久久久久久一区二区| 国产v综合v亚洲欧美久久| 日韩欧美国产骚| 欧美国产乱视频| 国产精品久久婷婷六月丁香|