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 ...
随机推荐
- log4net 性能测试
1.执行事务:20260 次 写日志: 耗时11.59分 不写日志: 耗时11.55分 异步日志: 耗时12.49分 (个人电脑,.net 线程池调用线程写日志可能比主线程直 ...
- 【poj3522-苗条树】最大边与最小边差值最小的生成树,并查集
题意:求最大边与最小边差值最小的生成树.n<=100,m<=n*(n-1)/2,没有重边和自环. 题解: m^2的做法就不说了. 时间复杂度O(n*m)的做法: 按边排序,枚举当前最大的边 ...
- 【Foreign】画方框 [主席树]
画方框 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output 输出一行一个整数,表示 CD 最多可能画了几个方框. Sam ...
- 【51NOD-0】1130 N的阶乘的长度 V2(斯特林近似)
[算法]数学 [题解]斯特林公式: #include<cstdio> #include<algorithm> #include<cmath> using names ...
- 课下加分项目 MYPWD 20155335 俞昆
Mypwd 的解读与实现 20155335 linux下pwd命令的编写 实验要求: 1 .学习pwd命令 2 . 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 .实现my ...
- hdu 1233 还是畅通工程 (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) ...
- 利用最新Apache解析漏洞(CVE-2017-15715)绕过上传黑名单
转载自:https://www.leavesongs.com/PENETRATION/apache-cve-2017-15715-vulnerability.html 目标环境: 比如,目标存在一个上 ...
- handle_level_irq 与handle_edge_irq 的区别【转】
转自:http://blog.csdn.net/xavierxiao/article/details/6087277 版权声明:本文为博主原创文章,未经博主允许不得转载. Linux 里, handl ...
- linux中字符串转换函数 simple_strtoul【转】
转自:http://blog.csdn.net/tommy_wxie/article/details/7480087 Linux内核中提供的一些字符串转换函数: lib/vsprintf.c [htm ...
- 【Android XML】Android XML 转 Java Code 系列之 Selector(2)
今天我们要把drawable下的selector的XML文件转换成Java代码.(打包进jar,不依赖apk) 在转换工具中的代码为: https://github.com/SickWorm/Andr ...