美文网首页
react-router源码分析-----------1.Lin

react-router源码分析-----------1.Lin

作者: iqing2012 | 来源:发表于2018-06-07 15:27 被阅读21次
    import React from "react";
    import PropTypes from "prop-types";
    import invariant from "invariant";
    import { createLocation } from "history";
    
    const isModifiedEvent = event =>
      !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
    
    /**
     * The public API for rendering a history-aware <a>.
     */
    class Link extends React.Component {
      static propTypes = {
        onClick: PropTypes.func,
        target: PropTypes.string,
        replace: PropTypes.bool,  
         //默认值是false,如果是真的,点击这个链接将会取代历史堆栈中的当前条目,而不是添加一个新的条目。
        to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
        //可能是字符串,可能是对象。
        //如果是对象可以包含 to={{
               pathname: '/courses',
               search: '?sort=name',
               hash: '#the-hash',
               state: { fromDashboard: true }
       
        innerRef: PropTypes.oneOfType([PropTypes.string, PropTypes.func])
        //允许访问组件的底层ref
      };
    
      static defaultProps = {
        replace: false
      };
    
      static contextTypes = {
        router: PropTypes.shape({
          history: PropTypes.shape({
            push: PropTypes.func.isRequired,
            replace: PropTypes.func.isRequired,
            createHref: PropTypes.func.isRequired
          }).isRequired
        }).isRequired
      };
    
      handleClick = event => {
        if (this.props.onClick) this.props.onClick(event);
    
        if (
          !event.defaultPrevented && // onClick prevented default
          event.button === 0 && // ignore everything but left clicks
          !this.props.target && // let browser handle "target=_blank" etc.
          !isModifiedEvent(event) // ignore clicks with modifier keys
        ) {
          event.preventDefault();
    
          const { history } = this.context.router;
          const { replace, to } = this.props;
    
          if (replace) {
            history.replace(to);
          } else {
            history.push(to);
          }
        }
      };
    
      render() {
        const { replace, to, innerRef, ...props } = this.props; // eslint-disable-line no-unused-vars
    
        invariant(
          this.context.router,
          "You should not use <Link> outside a <Router>"
        );
    
        invariant(to !== undefined, 'You must specify the "to" property');
    
        const { history } = this.context.router;
        const location =
          typeof to === "string"
            ? createLocation(to, null, null, history.location)
            : to;
    
        const href = history.createHref(location);
        return (
          <a {...props} onClick={this.handleClick} href={href} ref={innerRef} />
        );
      }
    }
    
    export default Link;
    

    相关文章

      网友评论

          本文标题:react-router源码分析-----------1.Lin

          本文链接:https://www.haomeiwen.com/subject/awiwsftx.html