前言

阮大师写入门教程能力一流。

首推它的Redux三篇入门文章。

http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_one_basic_usages.html

http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_two_async_operations.html

http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_three_react-redux.html

这三篇文章介绍了,

Redux的基本概念和API,异步操作,以及如何跟React相结合。

文章写得不错,但实践起来还是略显繁琐。

思考

下面提出我自己对Redux结合React使用的思考。

  1. 使用ramda库组合自定义中间件。

    这使得代码更灵活和透明。

  2. 异步操作也结合ramda库。

    可以不用引入第三方redux-thunkredux-promise中间件。

    使用ramda库更方便组合异步操作。

  3. state以React组件的state为准。

    redux的state只是辅助计算出React组件的state。

  4. 往store注入trigger函数,用来setState更新React组件。

在线demo

示例代码如下:

index.html

<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<h1>Hello world</h1>
<div id="app"></div>
<script src="index.js"></script>
</body> </html>

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import store from './store.js';
import {connect} from './connect.js';
class Test extends React.Component {
constructor(props) {
super(props);
this.state = {
value:0,
otherNum:0,
asyncing:false
};
}
dispatch(obj){
obj.state= this.state;
store.dispatch(obj);
}
setOtherData(){
this.dispatch({ type: 'Other' });
}
setData(){
this.dispatch({ type: 'INCREMENT' });
}
async(){
this.dispatch({type: 'Loading' });
this.dispatch({ type: 'ASYNC'});
}
componentDidMount(){
//store.dispatch({type: 'Init'})
}
render() {
const t = this;
console.log('render', store.getState(), t.state);
const {value ,asyncing,otherNum } = t.state;
return (
<div>
<div>数字:{value}</div>
<div onClick={t.setData.bind(this)}>点我+1</div>
{
(()=>{
if(asyncing){
return (<div>异步加载中</div>);
}
else{
return (<div onClick={t.async.bind(t)}>点我异步+10</div>);
}
})()
}
<br />
<div onClick={t.setOtherData.bind(this)}>点我其他数字+1</div>
<div>其他数字:{otherNum}</div> </div>);
}
}
const NewTest = connect(store)(Test) ; ReactDOM.render(
<NewTest />,
document.getElementById('app')
) module.exports = NewTest;

store.js

import { createStore } from 'redux';
import R from 'ramda'; const isPromise = function(e){
return !!e&&typeof e.then=="function";
}; const setTimeout1 = R.curry(function( state ){
return new Promise(function(resolve, reject){
setTimeout(function(){
resolve(state+ 10);
},1000);
})
}); // reducer,仅用于计算。
function counter(state = {}, action) {
if(isPromise(state)){
state = {};
}
state = R.merge(state, action.state);
console.log('reducer中的state',state);
switch (action.type) {
case 'Init':
return action.state;
case 'INCREMENT':
state.value = state.value + 1;
return state
case 'ASYNC':
return R.composeP( setTimeout1)(state.value);
case 'Loading':
return {asyncing: true}
case 'Other':
return {otherNum: state.otherNum+1};
default:
return state
}
} let store = createStore(counter); // subscribe,可用于执行含有副作用的操作。
store.subscribe((e) =>{
console.log('subscribe',store, store.getState(),e, this);
let state = store.getState();
if(isPromise(state)){
state.then(function(num){
store.trigger({value: num, asyncing:false });
})
}
else{
store.trigger(store.getState());
} }) module.exports = store;

connect.js

import {type} from 'ramda';

exports.connect = function (listenable, context) {
if(type(listenable) !== 'Object'){
throw new Error('connect function\'s argument is not a object');
}
listenable.trigger = function (obj,fn) {
this.setState(obj || {}, fn);
};
listenable.getReactState = function(){
return this.state;
};
return function(otherReactClass){
return class baseReactClass extends otherReactClass {
constructor(){
super();
}
componentDidMount(...args) {
context = context || this;
listenable.trigger = listenable.trigger.bind(context);
listenable.getReactState = listenable.getReactState.bind(context);
super.componentDidMount();
}
componentWillUnmount() {
listenable.trigger = null;
super.componentWillUnmount();
} }
}
}

草珊瑚的redux使用方式的更多相关文章

  1. 翻译 | Thingking in Redux(如果你只了解MVC)

    作者:珂珂(沪江前端开发工程师) 本文原创,转载请注明作者及出处. 原文地址:https://hackernoon.com/thinking-in-redux-when-all-youve-known ...

  2. redux 与 react-redux

    Redux 一.Redux 三大原则: 1.一个应用永远只有一个数据源(整个应用状态都保存在一个对象中,Redux提供的工具函数combineReducers可以解决庞大的数据对象的问题) 2.状态是 ...

  3. 我所遭遇过的游戏中间件---Redux

    我所遭遇过的游戏中间件---Redux 一.关于Redux Substance Redux 是一款纹理处理软件加中间件,专门用于纹理生成和压缩.具其用户指南介绍,它能够对纹理集进行优化,可以将现有压缩 ...

  4. redux详解

    redux介绍 学习文档:英文文档,中文文档,Github redux是什么 redux是一个独立专门用于做状态管理的JS库(不是react插件库),它可以用在react, angular, vue等 ...

  5. 前端AntD框架的upload组件上传图片时遇到的一些坑

    前言 本次做后台管理系统,采用的是 AntD 框架.涉及到图片的上传,用的是AntD的 upload 组件. 前端做文件上传这个功能,是很有技术难度的.既然框架给我们提供好了,那就直接用呗.结果用的时 ...

  6. React+DvaJS 之 hook 路由权限控制

    博客 学院 下载 GitChat TinyMind 论坛 APP 问答 商城 VIP 活动 招聘 ITeye 写博客 发Chat 登录注册 原 React+DvaJS 之 hook 路由权限控制 20 ...

  7. react的状态管理

    近两年前端技术的发展如火如荼,大量的前端项目都在使用或转向 Vue 和 React 的阵营, 由前端渲染页面的单页应用占比也越来越高,这就代表前端工作的复杂度也在直线上升,前端页面上展示的信息越来越多 ...

  8. react之传递数据的几种方式props传值、路由传值、状态提升、redux、context

    react之传递数据的几种方式 1.父子传值 父传值:<子的标签 value={'aaa'} index={'bbb'}></子的标签> 子接值:<li key={thi ...

  9. 使用redux代码文件的组织方式

    从架构触发,开始一个新应用的时候,代码文件的组织方式一定要考虑好 如果之前使用过mvc的框架那么对按角色组织方式一定不陌生 角色组织方式 reducer/ todoReducer.js filterR ...

随机推荐

  1. mac电脑复制粘贴使用command+c command+v

    mac电脑复制粘贴使用command+c command+v系统偏好设置--键盘--修饰键(右下角),将ctrl键和command键的功能对换一下即可用ctrl+c ctrl+v复制粘贴缺点:所有的c ...

  2. Lucene 个人领悟 (三)

    其实接下来就是贴一下代码,熟悉一下Lucene的正常工作流程,或者说怎么使用这个API,更深层次的东西这篇文章不会讲到. 上一篇文章也说了maven的配置,只要你电脑联网就可以下载下来.我贴一下代码. ...

  3. Codeforce 835B - The number on the board (贪心)

    Some natural number was written on the board. Its sum of digits was not less than k. But you were di ...

  4. a标签(普通标签如span)没有disabled属性 ,怎样利用js实现该属性

    a标签以及其她普通标签没有disabled属性,要想实现类似input框属性disabled可以通过css样式设置pointer-events的值来设定: <!DOCTYPE html> ...

  5. html介绍和head标签

      一.web标准 web准备介绍: w3c:万维网联盟组织,用来制定web标准的机构(组织) web标准:制作网页遵循的规范 web准备规范的分类:结构标准.表现标准.行为标准. 结构:html.表 ...

  6. 通过数组和枚举简化GPIO操作编码(转)

    源: 通过数组和枚举简化GPIO操作编码

  7. MySQL5.7 的新特点

    1.安全性 MySQL 5.7 的目标是成为发布以来最安全的 MySQL 服务器,其在 SSL/TLS 和全面安全开发方面有一些重要的改变. mysql.user表结构升级 MySQL5.7用户表my ...

  8. python之面向对象的高级进阶

    一 .isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class Foo(object ...

  9. 2018-2019-1 20189206 《Linux内核原理与分析》第五周作业

    linux内核分析学习笔记 --第四章 系统调用的三层机制 学习重点--系统调用 用户态.内核态和中断 Intel x86 CPU有四种不同的执行级别,分别是0,1,2,3其中数字越小,特权越高. L ...

  10. php 带省略号的分页

    原文链接:https://blog.csdn.net/u011060253/article/details/25308455 $curpage = isset($_GET[; $page = new ...