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);
Redux-react connect 源码自己重写的更多相关文章
- React Fiber源码分析 (介绍)
写了分析源码的文章后, 总觉得缺少了什么, 在这里补一个整体的总结,输出个人的理解~ 文章的系列标题为Fiber源码分析, 那么什么是Fiber,官方给出的解释是: React Fiber是对核心算法 ...
- 《React Native 精解与实战》书籍连载「React Native 源码学习方法及其他资源」
此系列文章将整合我的 React 视频教程与 React Native 书籍中的精华部分,给大家介绍 React Native 源码学习方法及其他资源. 最后的章节给大家介绍 React Native ...
- 【Orleans开胃菜系列2】连接Connect源码简易分析
[Orleans开胃菜系列2]连接Connect源码简易分析 /** * prism.js Github theme based on GitHub's theme. * @author Sam Cl ...
- React的React.createContext()源码解析(四)
一.产生context原因 从父组件直接传值到孙子组件,而不必一层一层的通过props进行传值,相比较以前的那种传值更加的方便.简介. 二.context的两种实现方式 1.老版本(React16.x ...
- React的React.createElement源码解析(一)
一.什么是jsx jsx是语法糖 它是js和html的组合使用 二.为什么用jsx语法 高效定义模版,编译后使用 不会带来性能问题 三.jsx语法转化为js语法 jsx语法通过babel转化为 ...
- React深入源码--了解Redux用法之Provider
在Redux中最核心的自然是组件,以及组件相关的事件与数据流方式.但是我们在Redux中并没有采用传统的方式在getInitialState()中去初始化数据,而是采用Provider统一处理,省去了 ...
- redux 中间件 --- applyMiddleware 源码解析 + 中间件的实战
前传 中间件的由来 redux的操作的过程,用户操作的时候,我们通过dispatch分发一个action,纯函数reducer检测到该操作,并根据action的type属性,进行相应的运算,返回st ...
- 精读《React PowerPlug 源码》
1. 引言 React PowerPlug 是利用 render props 进行更好状态管理的工具库. React 项目中,一般一个文件就是一个类,状态最细粒度就是文件的粒度.然而文件粒度并非状态管 ...
- React 16 源码瞎几把解读 【三 点 一】 把react组件对象弄到dom中去(矛头指向fiber,fiber不解读这个过程也不知道)
一.ReactDOM.render 都干啥了 我们在写react的时候,最后一步肯定是 ReactDOM.render( <div> <Home name="home&qu ...
随机推荐
- 「6月雅礼集训 2017 Day4」暴力大神hxx
[题目大意] 给出一个n重循环,每重循环有范围$[l, r]$,其中$l$,$r$可能是之前的变量,也可能是常数.求循环最底层被执行了多少次. 其中,保证每个循环的$l$,$r$最多有一个是之前的变量 ...
- lua中的继承
做为一个java出身的程序媛,长时间做Lua,重复一些工作后,特别想用继承.其实很简单.因为我有一大部分的场景,背景长的都一样,所以打算做一个父类. 需要注意的是,如果子类有和父类的同名函数,就会被覆 ...
- 计数排序的实现--适用于元素均较小的seq
今天无聊就打算把所有的排序算法都看一遍... 计数排序的时间复杂度是O(n),在算法导论中,用决策树模型中论证了,比较排序的情况为nlogn的复杂度.而计数排序的时间复杂度小于他的原因就是它不需要进行 ...
- java 和 JVM
C++和Java的区别 指针:java中不存在指针的概念,编程者无法直接通过指针来直接访问内存,有利于维护java程序的安全 多重继承:C++支持多重继承,java不支持多重继承,但是允许一个类继承多 ...
- OpenCV编程入门目录
第一部分 快速上手OpenCV 第1 章 邂逅OpenCV 图像处理.计算机视觉与OpenCV OpenCV 概述 起源及发展 应用概述 .2OpenCV 基本架构分析 .3OpenCV3 带来了什么 ...
- 初识ES6
1.ECMAScript的官网地址:http://www.ecma-international.org/cma-262/6.0/,其是JS语言的下一代标准,已经在2015年6月正式发布,目标是让JS可 ...
- System.getProperty方法中输出路径的方法
package codegenerator;/** *@author Eilen *@date 2017年9月27日---下午3:15:09 *@描述: *@answer */public class ...
- 利用h5,chart.js监测手机三轴加速度,用以研究计步算法等
用window.DeviceMotionEvent来判断手机浏览器是否支持访问硬件资源,window.addEventListener('devicemotion',deviceMotionHandl ...
- MySQL数据库分表分区(一)(转)
面对当今大数据存储,设想当mysql中一个表的总记录超过1000W,会出现性能的大幅度下降吗? 答案是肯定的,一个表的总记录超过1000W,在操作系统层面检索也是效率非常低的 解决方案: 目前针对 ...
- JVM内存分配与回收
1.内存分配与回收策略 内存自动管理:自动化的解决了对象内存分配和回收对象内存的问题. 一般在堆上分配对象,也可能经过JTI编译后间接在栈上分配. 主要分配在新生代的Eden区,如果启动了本地线程分配 ...