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);

Redux-react connect 源码自己重写的更多相关文章

  1. React Fiber源码分析 (介绍)

    写了分析源码的文章后, 总觉得缺少了什么, 在这里补一个整体的总结,输出个人的理解~ 文章的系列标题为Fiber源码分析, 那么什么是Fiber,官方给出的解释是: React Fiber是对核心算法 ...

  2. 《React Native 精解与实战》书籍连载「React Native 源码学习方法及其他资源」

    此系列文章将整合我的 React 视频教程与 React Native 书籍中的精华部分,给大家介绍 React Native 源码学习方法及其他资源. 最后的章节给大家介绍 React Native ...

  3. 【Orleans开胃菜系列2】连接Connect源码简易分析

    [Orleans开胃菜系列2]连接Connect源码简易分析 /** * prism.js Github theme based on GitHub's theme. * @author Sam Cl ...

  4. React的React.createContext()源码解析(四)

    一.产生context原因 从父组件直接传值到孙子组件,而不必一层一层的通过props进行传值,相比较以前的那种传值更加的方便.简介. 二.context的两种实现方式 1.老版本(React16.x ...

  5. React的React.createElement源码解析(一)

    一.什么是jsx  jsx是语法糖  它是js和html的组合使用  二.为什么用jsx语法 高效定义模版,编译后使用 不会带来性能问题 三.jsx语法转化为js语法  jsx语法通过babel转化为 ...

  6. React深入源码--了解Redux用法之Provider

    在Redux中最核心的自然是组件,以及组件相关的事件与数据流方式.但是我们在Redux中并没有采用传统的方式在getInitialState()中去初始化数据,而是采用Provider统一处理,省去了 ...

  7. redux 中间件 --- applyMiddleware 源码解析 + 中间件的实战

    前传  中间件的由来 redux的操作的过程,用户操作的时候,我们通过dispatch分发一个action,纯函数reducer检测到该操作,并根据action的type属性,进行相应的运算,返回st ...

  8. 精读《React PowerPlug 源码》

    1. 引言 React PowerPlug 是利用 render props 进行更好状态管理的工具库. React 项目中,一般一个文件就是一个类,状态最细粒度就是文件的粒度.然而文件粒度并非状态管 ...

  9. React 16 源码瞎几把解读 【三 点 一】 把react组件对象弄到dom中去(矛头指向fiber,fiber不解读这个过程也不知道)

    一.ReactDOM.render 都干啥了 我们在写react的时候,最后一步肯定是 ReactDOM.render( <div> <Home name="home&qu ...

随机推荐

  1. 在不安装Windows服务的情况下,如何进行调试或测试

    最近由于项目需要,写了几个Windows服务,可是如何对其进行测试呢? 如果通过命令Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe ...

  2. 自定义View的实现流程

    1.继承View组件,比如,LabelView继承了View   2.重写两个构造方法,比如,对于自定义View LabelView   LabelView(Context context),如果该自 ...

  3. 【51NOD-0】1008 N的阶乘 mod P

    [算法]简单数学 [题解]多项式展开:(a*b)%p=(a%p*b%p)%p #include<cstdio> #include<algorithm> #define rep( ...

  4. Zabbix 通过 JMX 监控 java 进程

    参考: [ JMX monitoring ] [ Zabbix Java gateway ] [ JMX Monitoring (Java Gateway) not Working ] [ Monit ...

  5. appcan UI

    appcan UI 公共类 ([appcanUI框架地址:](http://newdocx.appcan.cn/UI/source) .ub { display: -webkit-box !impor ...

  6. textarea输入框实时统计输入字符数

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 前端—css

    css css概述 CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一.css的四种引入方式: 1.行内式   ...

  8. 《gdb调试之基础篇》

    <gdb调试之基础篇> http://blog.csdn.net/miss_acha/article/details/42346543

  9. python string 对齐文本的几个方法

    用rjust().ljust()和center()方法对齐文本

  10. C基础入门 - 第一章 - C语言绪言

    第1章 C语言绪言 1.1 C语言概述 1.1.1 C语言世界 1.1.2 C语言学习, 能当饭吃吗 1.2 开发环境构建 1.2.1 visual studio安装使用 1.2.2 visual s ...