编程语言
首页 > 编程语言> > 158、react-redux.js源码中,与mapDispatchToProps相关的源码

158、react-redux.js源码中,与mapDispatchToProps相关的源码

作者:互联网

1、正常执行
(1)connect(mapStateToProps, mapDispatchToProps, mergeProps)
(2)match(mapDispatchToProps, mapDispatchToPropsFactories, 'mapDispatchToProps')
2、引用的函数和变量
(1)1-2中的match
function match(arg, factories, name) {
    for (var i = factories.length - 1; i >= 0; i--) {
      var result = factories[i](arg);
      if (result) return result;
    }

}
(2)1-2中的mapDispatchToPropsFactories
mapDispatchToPropsFactories = defaultMapDispatchToPropsFactories;

(3)2-2中的defaultMapDispatchToPropsFactories
defaultMapDispatchToPropsFactories = [whenMapDispatchToPropsIsFunction, whenMapDispatchToPropsIsMissing, whenMapDispatchToPropsIsObject];
(4)2-3中的whenMapDispatchToPropsIsFunction
function whenMapDispatchToPropsIsFunction(mapDispatchToProps) {
    return typeof mapDispatchToProps === 'function' ? wrapMapToPropsFunc(mapDispatchToProps, 'mapDispatchToProps') : undefined;
}
(5)2-3中的whenMapDispatchToPropsIsMissing
function whenMapDispatchToPropsIsMissing(mapDispatchToProps) {
    return !mapDispatchToProps ? wrapMapToPropsConstant(function (dispatch) {
        return {dispatch: dispatch};
    }) : undefined;
}
(6)2-3中的whenMapDispatchToPropsIsObject
function whenMapDispatchToPropsIsObject(mapDispatchToProps) {
    return mapDispatchToProps && typeof mapDispatchToProps === 'object' ? wrapMapToPropsConstant(function (dispatch) {
        return redux.bindActionCreators(mapDispatchToProps, dispatch);
    }) : undefined;
}
(7)2-4中的wrapMapToPropsFunc
function wrapMapToPropsFunc(mapToProps, methodName) {
    return function initProxySelector(dispatch, _ref) {
        var displayName = _ref.displayName;

        var proxy = function mapToPropsProxy(stateOrDispatch, ownProps) {
            return proxy.dependsOnOwnProps ? proxy.mapToProps(stateOrDispatch, ownProps) : proxy.mapToProps(stateOrDispatch);
        };

        // allow detectFactoryAndVerify to get ownProps
        proxy.dependsOnOwnProps = true;

        proxy.mapToProps = function detectFactoryAndVerify(stateOrDispatch, ownProps) {
            proxy.mapToProps = mapToProps;
            proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps);
            var props = proxy(stateOrDispatch, ownProps);

            if (typeof props === 'function') {
                proxy.mapToProps = props;
                proxy.dependsOnOwnProps = getDependsOnOwnProps(props);
                props = proxy(stateOrDispatch, ownProps);
            }

            verifyPlainObject(props, displayName, methodName);

            return props;
        };

        return proxy;
    };
}

(8)2-5,2-6中的wrapMapToPropsConstant
function wrapMapToPropsConstant(getConstant) {
    return function initConstantSelector(dispatch, options) {
        var constant = getConstant(dispatch, options);

        function constantSelector() {
            return constant;
        }

        constantSelector.dependsOnOwnProps = false;
        return constantSelector;
    };
}
(9)2-6中的bindActionCreators
function bindActionCreators(actionCreators, dispatch) {
  if (typeof actionCreators === 'function') {
    return bindActionCreator(actionCreators, dispatch);
  }
  if (typeof actionCreators !== 'object' || actionCreators === null) {
    throw new Error('bindActionCreators expected an object or a function, instead received ' + (actionCreators === null ? 'null' : typeof actionCreators) + '. ' + 'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?');
  }
  var keys = Object.keys(actionCreators);
  var boundActionCreators = {};
  for (var i = 0; i < keys.length; i++) {
    var key = keys[i];
    var actionCreator = actionCreators[key];
    if (typeof actionCreator === 'function') {
      boundActionCreators[key] = bindActionCreator(actionCreator, dispatch);
    }
  }
  return boundActionCreators;
}
(10)2-9中的bindActionCreator
function bindActionCreator(actionCreator, dispatch) {
  return function () {
    return dispatch(actionCreator.apply(undefined, arguments));
  };
}

 

 

Object.create =  function (o) {
    var F = function () {};
    F.prototype = o;
    var newObj=new F();
    return newObj;
};

var inherits = function (subClass, superClass) {
        if (typeof superClass !== "function" && superClass !== null) {
            throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
        }

        subClass.prototype = Object.create(superClass && superClass.prototype, {
            constructor: {
                value: subClass,
                enumerable: false,
                writable: true,
                configurable: true
            }
        });
        if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
};

 

标签:function,return,mapDispatchToProps,158,dispatch,源码,proxy,var
来源: https://www.cnblogs.com/gushixianqiancheng/p/11949610.html