redux-simple 简化版的redux
作为react的粉丝,当然要吐槽一下react组件通信问题。react的单向数据流是组件通信的一大阻碍,只允许父组件向子组件传值,子组件向父组件传值只能通过父组件向子组件传递回调函数实现。如果在深层次的组件结构当中,复杂与繁多的回调会大大增加程序的维护难度与可读性,延长开发周期。即使react实现的再优秀,组件通信没处理好,还是一坨屎。redux给react的组件通信带来解决方案:构建一个大的状态作用域(一个大组件),大组件里的所有组件状态变化都只需要维护大组件,更新的状态都有大组件开始下发,这样就避免了子组件向父组件传值需要回调方法的问题。
redux优点:贯彻了react的单向数据流思想,避免父子组件通信时繁琐的回调,清晰的数据流简化了业务逻辑实现,提高应用维护效率。
redux槽点:为了数据中心化与自动化测试分离出了action与redcuer与mapStateToProps,简单的数据操作被复杂化了。每次dispatch都会重新渲染状态作用域里的所有组件,虽然react使用diff算法筛选出需要改变的节点进行渲染(优化了性能),但是在庞大以及深层次的组件结构中,diff带来的消耗还是不可忽视的。
我格外厌恶redux繁琐的操作,所以自己实现了一个简化版的redux。把action与redcuer合并、废弃了mapStateToProps简化了redux的数据操作。由于action与redcuer的合并后存在数据依赖,所以合并后的action不能做数据自动化测试。除了简化redux的繁琐的数据操作之外,还通过监听组件里的动态数据与dispatch更新的数据对比检测该组件是否需要重新渲染,解决redux最大的弊端【redux组件渲染弊端:redux dispatch后没有变化的组件也会被重新渲染,这是不必要的性能消耗】。我还丰富了Component基类给每个组件提供了重新渲染检测功能。
redux-simple.s实现如下:
import React,{Component,createElement} from "react";
var event=function(){
var propsment={};
var dispatchProps={};
return {
//添加监听属性
listenProps:function(props,_this){
if(_this.constructor&&_this.constructor.name){
propsment[_this.constructor.name]=props;
}else{
throw new Error("监听对象不是react组件");
}
},
//清空监听的属性
emptyProps:function(){
propsment={};
},
//检测是否需要更新
checkProps:function(_this){
var data=propsment[_this._reactInternalInstance.getName()]||[];
var propKeys=Object.keys(dispatchProps);
//没有监听属性的组件直接更新
if(!data.length){
return true;
}
for(var i=0;i<data.length;i++){
//判断监听的属性是否存在于更新数据里面,存在则更新组件
if(propKeys.indexOf(data[i])!=-1){
return true;
}
}
return false;
},
//监听更新的数据
listenDispatch:function(props){
dispatchProps=props;
}
}
}()
export function connect(App,actions){
let dispatch,getState;
dispatch=getState=function(){
throw new Error("组件还没初始化");
}
class Main extends Component{
constructor(props){
super(props);
this.constructor.setState=dispatch=(data)=>{
//监听更新的数据
event.listenDispatch(data);
//更新store
this.setState(Object.assign({},this.state,data));
}
getState=()=>{
return Object.assign({},this.state);
}
this.actions=bindAction(actions,dispatch,getState||{});
}
render(){
return <div>
{createElement(App,Object.assign({dispatch:dispatch,listenProps:event.listenProps,getState:getState,actions:this.actions},this.props,this.state))}
</div>
}
}
return Main;
}
//action方法绑定dispatch并支持异步
function bindActionCreators(action,dispatch,getState){
if(typeof action==="function"){
return function(){
[].unshift.call(arguments,getState());
var newState=action.apply(null,arguments);
if(typeof newState==="function"){
newState(dispatch)
}else if(isObject(newState)){
dispatch(newState);
}else{
throw new Error("action方法返回值只能是函数或者对象");
}
}
}
}
//给所有的action方法绑定dispatch
export function bindAction(actions,dispatch,getState){
var newActions={};
if(isObject(actions)){
var keys=Object.keys(actions);
keys.forEach(function(value){
newActions[value]=bindActionCreators(actions[value],dispatch,getState)
})
return newActions;
}else if(actions){
throw new Error("actions必须是对象")
}
}
//检测是否是一个对象
let isObject=(data)=>{
if(Object.prototype.toString.call(data)=="[object Object]"){
return true;
}else{
return false;
}
}
//构建新的组件基类
export default class NewCom extends Component{
shouldComponentUpdate(nextProps){
return event.checkProps(this,nextProps); //检测是否重新渲染该组件
}
componentWillUnmount(){
event.emptyrProps(); //清空监听列表
}
}
使用实例如下:
import ReactDom from "react-dom";
import React from "react";
import Component,{connect} from "./react-extend.js";
import * as actions from "./action/test.js";
class Test01 extends Component{
constructor(props){
super(props);
this.props.listenProps(["test01"],this); //监听该组件的动态数据
}
render(){
return <h1>{this.props.test01}</h1>
}
}
class Test02 extends Component{
constructor(props){
super(props);
this.props.listenProps(["test02"],this); //监听该组件的动态数据
}
clickHandler(){
this.props.actions.changeTest01(789);
}
render(){
return <h1 onClick={this.clickHandler.bind(this)}>{this.props.test02}</h1>
}
}
class Main extends Component{
componentDidMount(){
this.props.actions.init({
test01:123,
test02:456,
})
}
render(){
return <div>
<Test01 {...this.props}/>
<Test02 {...this.props}/>
</div>
}
}
var NewMain=connect(Main,actions);
ReactDom.render(
<NewMain />,
document.getElementById("container")
)
action:
export function changeTest01(state,value){
return {
test01:value,
}
}
export function init(state,obj){
return obj;
}
redux-simple 简化版的redux的更多相关文章
- redux源码解析-redux的架构
redux很小的一个框架,是从flux演变过来的,尽管只有775行,但是它的功能很重要.react要应用于生成环境必须要用flux或者redux,redux是flux的进化产物,优于flux. 而且r ...
- react第十六单元(redux的认识,redux相关api的掌握)
第十六单元(redux的认识,redux相关api的掌握) #课程目标 掌握组件化框架实现组件之间传参的几种方式,并了解两个没有任何关系组件之间通信的通点 了解为了解决上述通点诞生的flux架构 了解 ...
- [Redux + Webpack] Hot reloading Redux Reducers with Webpack
Webpack will hot reload the component, but the reducer we need hard refresh. To sovle the problem, g ...
- Redux学习笔记:Redux简易开发步骤
该文章不介绍Redux基础,也不解释各种乱乱的概念,网上一搜一大堆.只讲使用Redux开发一个功能的步骤,希望可以类我的小白们,拜托它众多概念的毒害,大牛请绕道! 本文实例源代码参考:React-Re ...
- redux sample with slim redux source code
code sample没有package.json文件,也就没有任何外部依赖,直接使用slim redux source code. slim resux只有90多行. nodejs对es6的impo ...
- [Redux] Important things in Redux
Root Smart component can be overloaded, divide 'smart' component wisely & using Provider. Proble ...
- 1.Redux学习1,Redux
Redux流程图如上: Action就是一条命令 Store顾名思义就是存储数据的, Reducers是一个回调函数用于处理数据,它处理完数据会返回给Store存储起来 基本流程就是:组件中用Stor ...
- 4 react 简书 引入 redux 的 combineReducers 对 redux 数据进行管理
1. src 下的 common 下的 header 创建 store 文件夹 下创建 reducer.js # src/common/header/store/reducer.js const st ...
- React之redux学习日志(redux/react-redux/redux-saga)
redux官方中文文档:https://www.redux.org.cn/docs/introduction/CoreConcepts.html react-redux Dome:https://co ...
随机推荐
- sock_ntop等函数
inet_ntop的一个基本问题是:它要求调用者传递一个指向某个二进制地址的指针, 而该地址通常包含在一个套接字地址结构中,这就要求调用者必须知道这个结构的格式和地址簇, 为了使用这个函数,我们必须为 ...
- Android 判断当前网络连接类型
实际应用开发时,如果存在需要用户获取大量数据的情况,最好是先判断下网络类型,提示用户当前的网络类型,是否需要连接Wifi,etc.(手机流量太贵啦,当然土豪是无视这玩意的, (/ □ \)). 定义网 ...
- [转]C# FileSystemWatcher监控指定文件或目录的文件的创建、删除、改动、重命名等活动
觉得这个很常用..比如一些软件. http://www.rabbit8.cn/DoNet/407.html FileSystemWatcher控件主要功能: 监控指定文件或目录的文件的创建.删 ...
- IIS的安装与配置详细图解教程。
一.这里讲的是在WINDOWS 2003下的IIS组件的安装,至于WINDOWS XP,请看这里: 开始-控制面板-添加或删除程序-添加/删除windows组件 勾选应用程序服务器 勾选I ...
- 异步导入导出Redis数据(利用Hiredis、Libevent)
最近工作中需要用到一个将数据从Redis导出到文本(或从文本导入Redis)的工具.找到一个用Ruby写的开源软件redis-dump(http://delanotes.com/redis-dump/ ...
- redis 扩展安装使用
Redis的php客户端库非常之多, Redis推荐客户端链接是:http://redis.io/clients 推荐用phpredis,下载地址:https://github.com/nicolas ...
- HDU 2389 Rain on your Parade
大意:在一个二维坐标系上有nx个人和ny把伞,每个人都有自己的移动速度,问有多少人可以再 time 时间内移动到不同的雨伞处(不允许两个人共用一把伞). 输入数据: 第一行是一个T代表T组测试数据 ...
- 【转】Eclipse导入library的时候报:Found 2 versions of android-support-v4.jar in the dependency list
原文网址:http://www.07net01.com/2015/03/779691.html 错误类型:Eclipse导入library的时候报:Found 2 versions of androi ...
- Matchers and assertthat
assertThat的一般句型为: assertThat([value],[matcher statement]); 这种断言的优点有: 1.更具有可读性:该语法允许我们以“主谓宾”的方式来思考(as ...
- Lenovo G40-30 Install win7
SATA Controller mode 两种硬盘模式,如果装XP应该要选第二种模式,兼容:据说ACHI比IDE模式性能好,开机能快点, 装Win7,两种模式都可以,只不过装Win7一般人都用ACHI ...