• Redux-react connect 源码自己重写


    import Counter from '../components/Counter';
    import { increment, decrement, incrementIfOdd, incrementAsync } from '../actions';
    import { bindActionCreators } from 'redux';
    import React, { Component, createElement } from 'react';
    import PropTypes from 'prop-types';
    import hoistStatics from 'hoist-non-react-statics'
    import * as ActionCreators from '../actions';
    
    const storeShape = PropTypes.shape({
      subscribe: PropTypes.func.isRequired,
      dispatch: PropTypes.func.isRequired,
      getState: PropTypes.func.isRequired
    });
    
    const connect = function(mapStateToProps, mapDispatchToProps){
        return function(WrappedComponent){
            class Connect extends Component {
                constructor(props, context) {
                  super(props, context)
                  this.store = props.store || context.store;
    
                  const storeState = this.store.getState()
                  this.state = { storeState }
                }
    
                handleChange() {
                  const storeState = this.store.getState();
                  this.setState({ storeState });
                }
    
                trySubscribe() {
                    this.store.subscribe(this.handleChange.bind(this))
                    this.handleChange();
                }
    
                componentDidMount() {
                    this.trySubscribe();
                }
    
                render() {
                    let store =  this.store;
                    let dispatch = store.dispatch;
                    let mergedProps = {};
                    
                    let dispatchProps;
                    // 若它是个函数
                    if (typeof mapDispatchToProps == 'function'){
                        dispatchProps = mapDispatchToProps;
                    } else if(!mapDispatchToProps){
                        // 若没有传递参数
                        dispatchProps = dispatch => ({ dispatch });
                    } else {
                        // 若 mapDispatchToProps 是一个对象
                        const wrapActionCreators = function(actionCreators) {
                          return function (dispatch) {
                            return bindActionCreators(actionCreators, dispatch);
                          };
                        }
                        dispatchProps = wrapActionCreators(mapDispatchToProps);
                        
                        // 若它是个对象, 属性值 是一个action create, 类似
                        // for(let j in mapDispatchToProps){
                        //     dispatchProps[j] = () => {
                        //         dispatch(mapDispatchToProps[j]());
                        //     }
                        // }
                    }
                    dispatchProps = dispatchProps(dispatch);
                    
                    let stateProps = mapStateToProps( this.state.storeState );
                    for(let i in stateProps){
                        mergedProps[i] = stateProps[i];
                    }
    
                    for(let i in dispatchProps){
                        mergedProps[i] = dispatchProps[i];
                    }
    
                    return createElement(WrappedComponent,
                        mergedProps
                    );
                }
            }
    
            Connect.contextTypes = {
              store: storeShape
            }
            Connect.propTypes = {
              store: storeShape
            }
            return hoistStatics(Connect, WrappedComponent);
        }
    }
    
    export default connect(
      state => ({ counter: state.counter }),
      /*
          注意这里写成如下形式不会被执行
          dispatch => ({
            increment: increment,
            decrement: decrement,
            incrementIfOdd: incrementIfOdd,
            incrementAsync: incrementAsync,
          })
      */
      // dispatch => ({
      //   increment: () => dispatch( increment() ),
      //   decrement: () => dispatch( decrement() ),
      //   incrementIfOdd: () => dispatch( incrementIfOdd() ),
      //   incrementAsync: () => dispatch( incrementAsync() )
      // })
      // ActionCreators
      dispatch => bindActionCreators(ActionCreators, dispatch)
    )(Counter);
  • 相关阅读:
    DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)
    MySQL聚合函数、控制流程函数(含navicat软件的介绍)
    MySQL JDBC事务处理、封装JDBC工具类
    ResultSet详解(转)
    开源软件收集
    免费开源3D模型设计软件汇总
    oschina多媒体工具
    oschina文件传输
    简单工厂模式
    权限表的设计
  • 原文地址:https://www.cnblogs.com/zhengming2016/p/6853005.html
Copyright © 2020-2023  润新知