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

首頁 > 編程 > JavaScript > 正文

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

2019-11-19 11:24:18
字體:
來源:轉載
供稿:網友

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的運行機制,如果有什么錯誤還望指出,謝謝

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
色妞一区二区三区| 爽爽爽爽爽爽爽成人免费观看| 亚洲国产一区二区三区四区| 日韩精品免费在线观看| 成人免费视频xnxx.com| 亚洲精品久久久久中文字幕二区| 国产91免费看片| 韩国视频理论视频久久| 国产欧美日韩专区发布| 日本老师69xxx| 国产精品成人免费视频| 亚洲自拍偷拍色片视频| 奇米4444一区二区三区| 日韩中文字幕在线| 亚洲性视频网站| 成人黄色激情网| 91av网站在线播放| 66m—66摸成人免费视频| 久久男人av资源网站| 国产精品日韩在线一区| 欧美美女操人视频| 亚洲视频在线观看视频| 国产精品扒开腿做爽爽爽视频| 中文在线不卡视频| 欧美激情一级精品国产| 成人www视频在线观看| …久久精品99久久香蕉国产| 国产成人免费av电影| 国产午夜精品视频免费不卡69堂| 动漫精品一区二区| 91精品国产综合久久久久久蜜臀| 91sa在线看| 亚洲免费av电影| 日韩av电影免费观看高清| 黄色一区二区三区| 中国日韩欧美久久久久久久久| 国外色69视频在线观看| 一区二区三区黄色| 久久99热这里只有精品国产| 国产999在线| 日韩福利在线播放| 亚洲精品动漫久久久久| 亚洲日韩欧美视频| 午夜精品久久久久久久99热浪潮| 精品国产精品三级精品av网址| 亚洲免费小视频| 中文字幕日韩av| 亚洲精品有码在线| 日韩中文字幕在线免费观看| 综合av色偷偷网| 久久精品电影网| 91大神福利视频在线| 欧美人在线观看| 尤物99国产成人精品视频| 欧美午夜片欧美片在线观看| 精品小视频在线| 中文字幕欧美专区| 久久久久免费精品国产| 国产精品偷伦免费视频观看的| 日韩av最新在线| 中国日韩欧美久久久久久久久| 日韩h在线观看| 精品国产乱码久久久久久虫虫漫画| 成人亚洲综合色就1024| 久久精品国产亚洲7777| 亚州精品天堂中文字幕| 一区二区欧美久久| 国产精品国产三级国产aⅴ9色| 久久色在线播放| 神马国产精品影院av| 久久久久久久久久婷婷| 亚洲人高潮女人毛茸茸| 国内精品伊人久久| 国产婷婷97碰碰久久人人蜜臀| 欧美性xxxx极品高清hd直播| 亚洲色图35p| 亚洲性日韩精品一区二区| 亚洲最大的av网站| 国产成人久久久精品一区| 国自产精品手机在线观看视频| 国产日韩精品一区二区| 亚洲男人天堂手机在线| 欧美日韩国产色视频| 夜夜嗨av色综合久久久综合网| 成人福利在线视频| 日韩高清免费观看| 欧美一级大片在线免费观看| 成人精品aaaa网站| 一本一本久久a久久精品牛牛影视| 欧美丝袜第一区| 日韩在线观看免费全集电视剧网站| 日本不卡免费高清视频| 欧美另类在线播放| 亚洲电影免费观看高清完整版在线| 一区二区三区视频在线| 欧美性猛交丰臀xxxxx网站| 成人网欧美在线视频| 亚洲激情成人网| 中文在线资源观看视频网站免费不卡| 国语自产精品视频在线看一大j8| 欧美影院在线播放| 欧美性在线观看| 国内精品久久久久久中文字幕| 欧美在线中文字幕| 国外成人在线播放| 欧美性猛交xxxx黑人| 影音先锋欧美精品| 日韩美女免费视频| 欧美精品在线极品| 日本久久精品视频| 久久精品视频一| 美女av一区二区三区| 欧美乱妇40p| 欧美日韩第一页| 欧美国产亚洲精品久久久8v| 亚洲第一色中文字幕| 国产区精品视频| 日韩精品在线免费观看视频| 国产精品自产拍在线观| 91影院在线免费观看视频| 狠狠躁夜夜躁人人躁婷婷91| 国产女人精品视频| 日本欧美爱爱爱| 国产在线视频2019最新视频| 狠狠综合久久av一区二区小说| 一本一本久久a久久精品牛牛影视| 亚洲国产中文字幕在线观看| 国产精品高潮粉嫩av| 久久精品视频亚洲| 欧美尺度大的性做爰视频| 成人在线中文字幕| 永久免费毛片在线播放不卡| 国产精品狠色婷| 欧美尺度大的性做爰视频| 国产精品爽爽ⅴa在线观看| 亚洲国产中文字幕久久网| 亚洲自拍av在线| 久久91亚洲精品中文字幕| 欧美成年人视频| 亚洲乱码国产乱码精品精天堂| 亚洲精品美女久久久| 国产精品天天狠天天看| 最好看的2019的中文字幕视频| 亚洲欧美国产va在线影院| 中文字幕精品视频| 成人在线视频网| 欧美俄罗斯性视频| 亚洲精品动漫100p| 欧美一乱一性一交一视频| 91探花福利精品国产自产在线| 日韩在线免费视频观看| 精品国产1区2区| 成人免费淫片aa视频免费| 精品久久久久久久久久久久| 68精品国产免费久久久久久婷婷| 亚洲黄色免费三级| 国产成人中文字幕| 精品日本高清在线播放| 欧美亚州一区二区三区| 国产成人在线精品| 欧美激情视频一区二区| 日韩久久精品成人| 欧美国产日韩一区二区在线观看| 国产深夜精品福利|