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. c# socket select 模型代码(u3d)

    其实写过多次网络链接.但是因为换了工作,又没电脑在身边,所以以前的代码都没办法翻出来用. 所以从今天起,一些常用的代码只好放到网上. 公司有一个局域网的游戏.本来想用u3d的rpc就可以完成.但是后来 ...

  2. POJ 2533 Longest Ordered Subsequence LIS O(n*log(n))

    题目链接 最长上升子序列O(n*log(n))的做法,只能用于求长度不能求序列. #include <iostream> #include <algorithm> using ...

  3. 【CC2530入门教程-01】CC2530微控制器开发入门基础

    [引言] 本系列教程就有关CC2530单片机应用入门基础的实训案例进行分析,主要包括以下6部分的内容:[1]CC2530微控制器开发入门基础.[2]通用I/O端口的输入和输出.[3]外部中断初步应用. ...

  4. Java多线程学习(六)Lock锁的使用

    系列文章传送门: Java多线程学习(二)synchronized关键字(1) Java多线程学习(二)synchronized关键字(2) Java多线程学习(三)volatile关键字 Java多 ...

  5. css position的值

    值 描述 absolute 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位. 元素的位置通过 "left", "top", " ...

  6. Python标准库笔记(2) — re模块

    re模块提供了一系列功能强大的正则表达式(regular expression)工具,它们允许你快速检查给定字符串是否与给定的模式匹配(match函数), 或者包含这个模式(search函数).正则表 ...

  7. SUSE 11.3 linux ISO下载地址

    http://linux.iingen.unam.mx/pub/Linux/Suse/isos/SLES11/ SLE-11-SP3-SDK-DVD-i586-GM-DVD1.iso 6deaa960 ...

  8. v4l2驱动编写篇【转】

    转自:http://blog.csdn.net/michaelcao1980/article/details/53008418 大部分所需的信息都在这里.作为一个驱动作者,当挖掘头文件的时候,你可能也 ...

  9. #error This file was generated by a newer version of protoc

    pattern@pattern89:/raid0/workspace/houjun/caffe-ssd$ sudo make all -j8PROTOC src/caffe/proto/caffe.p ...

  10. inux权限管理(1)

    1.linux系统文件普通权限 2.文件所属主的设置,组的指定 3.特殊权限 4.acl权限 5.su命令及其注意事项和sudo权限 6.权限管理的注意点 0.首先,在linux下用户账户是分角色的, ...