react-native 从简单的事件分发来介绍redux
来源:互联网 发布:如东知品服饰有限公司 编辑:程序博客网 时间:2024/05/21 23:33
载请注明出处:王亟亟的大牛之路
这两天组里来了几个新人,有的用过redux,有的没用过,为了让他们上手或者理解的更透彻,就写了个demo,代码逻辑来源于https://github.com/ninty90/react-native-redux-demo
开篇前先安利
Android:https://github.com/ddwhan0123/Useful-Open-Source-Android
React-native:https://github.com/ddwhan0123/Useful-Open-Source-React-Native
源码地址:https://github.com/ddwhan0123/ReduxDemo
演示效果:
理论知识:
http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_one_basic_usages.html
http://www.jianshu.com/p/0e42799be566
http://www.jianshu.com/p/3334467e4b32
理论知识重复炒冷饭等于制造网络垃圾,所以贴上几篇我觉得写得不错的给大家瞅瞅
核心理念:
Store:应用只有一个单一的 Store,State是这个状态集合某一时刻的状态
Action:改变state的载体,也是Store的数据源
Reducer:更新Store的具体操作者
ok,你现在肯定云里雾里的,我们用代码边写边解释
项目结构:
Action相关
MathType
- 1
- 2
- 3
- 1
- 2
- 3
这里是2个常量,”加类型”,”减类型”,我们每种action都有他相对应的类型,可以写在Action里也可以写一个类型对他进行划分,我习惯是拆的越细越好
MathAction
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
Reducer相关
MathReducer
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
肉眼看起来很简单,这里接受两种类型的action一个是➕,一个是➖,每次他都会改变传入参数的值,而且是一定改变,一定会+10或者-10!
reducer只是一个方法,传入什么,返回什么。结果是个恒定值,只要传入参数不变,返回参数一定也不变!
reducers
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
这是一个reducer的大容器,你所有reducer丢一个方法里也不是不能处理,但是性能差加难以维护,所以redux提供combineReducers来帮你整合reducer
Store相关
store是个应用级持有的对象,所以我们把他放到了”根”页面里去做初始化,因为我们之后还会用到异步action,所以还用到redux-thunk
的相关内容
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
只需要在外面套一层,所有子控件的属性竟在掌握!
页面代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
这样这个简单的demo就讲完了,什么?看不懂,我也觉得 这说的是啥啊,过程都没讲清楚,ok 看下console你就明白了!
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
action reducer本身看起来平淡无奇,但是在store内轮转使得我们省去了大量setState的人工操作,避免了各种不可描述的render().
但是 redux处理不好会各种多次render页面,之后的文章我会讲一讲异步的action和react-native优化
我是王亟亟!我们下篇见
有问题可以扫码加好友询问噢,非公众号,闲人勿扰!
- react-native 从简单的事件分发来介绍redux
- react-native 从简单的事件分发来介绍redux
- react-native中TouchableXXX事件的分发
- 【Android事件分发】从简单的onTouch理解Android事件分发
- react-native 简单的react-redux创建
- react-redux,redux,react native之间的关系
- react-native Touch事件的拦截与分发
- [React Native]Redux的基本使用方式
- [React Native]Redux的基本使用方式
- 基于 Redux 的 React Native 应用架构
- React-Native Redux的基本使用方式
- react native redux
- react native redux counter
- react-native redux debug
- React Native集成Redux
- React Native 之 Redux
- react native and redux
- react-native redux学习心得
- python快速入门(4)判断结构
- 二叉树的层序遍历详细讲解(附完整C++程序)
- 机器学习入门基础文章
- CentOS6.7成功安装sbt
- POJ 2195 Going Home <最小费用最大流>
- react-native 从简单的事件分发来介绍redux
- Codeforces Round #422 (Div. 2) A. I'm bored with life
- mysql储存过程遇到的坑(while 循环中,select count(*) 总是返回0的解决方案 )
- 我的第一个Hbuilder项目
- Java连接数据库
- sizeof的那些事儿
- 利用Immutable解决React-Native那些因为对象被篡改导致的多次render问题
- STM8S TIM1/TIM2 初始化函数
- Html标签解释及使用方法