react-native添加redux支持
来源:互联网 发布:python 函数传入列表 编辑:程序博客网 时间:2024/05/19 22:56
redux简介
redux是一个用于管理js应用状态的容器。redux出现时间并不是很长,在它出现之前也有类似功能的模块出现,诸如flux等等。redux设计的理念很简单,似乎最初这个开发团队就有让redux可以方便融入在server, browser, mobile client端的打算。目前在github上redux-*的第三方中间件、插件越来越多。如果React项目中想使用redux,那么就有react-redux插件来完成配合。
转自:http://blog.csdn.net/xiangzhihong8/article/details/71512756
项目实例
如图所示,这是一个非常简单的例子:只有两个文件package.json和index.iOS.js, 点击加1按钮数字值就会+1, 点击减1按钮数字值就会-1, 点击归零按钮则数字值置为0。
index.ios.js代码如下:
- 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
添加redux
1,要想使用redux的相关功能,首先需要添加redux相关依赖库。直接使用npm install 命令安装。默认情况下会将安装的信息保存到package.json里面。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2,创建actionsTypes.js用来定义所有的action名称, 定义三个常量action, 分别表示增加、减小、重置。
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
actions.js代码如下:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
3,创建reducers.js, 根据需要在收到相关的action时操作项目的state。
- 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
4,创建store.js。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
至此,redux的几大部分都创建完毕, 下一步就是引入项目中。
5,在项目中引入上面的文件。创建app.js和home.js
app.js代码如下:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
修改home.js
- 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
- 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
最后在index.ios.js添加app引入。
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
这样,我们就将redux引入到了React Native中。commond+R运行, command+D打开chrome浏览器调试, 可以看到redux-logger把每个action动作都打和state的前后变化印出来。
参考:深入理解redux
- react-native添加redux支持
- 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学习心得
- react-native + redux 实践
- React Native 集成 Redux
- React-Native 之 redux 与 react-redux
- React-Native 之 redux 与 react-redux
- React Native 向iOS项目中添加React Native支持
- android 添加React Native支持更新版
- react-native,redux,redux-saga组合开发
- react native 中 使用 Redux
- react-native 之redux install
- maven相关整理(国内镜像、代理设置)
- 从本地加载图片和drawable加载到布局格式不一样的问题
- 对数函数应用实例
- 【C语言】C语言编写扫雷
- 事件拦截机制分析——Android群英传
- react-native添加redux支持
- 项目目标和范围管理 技能点一 二 三
- gradle升级,v7包莫名其妙的报错 解决方法
- 2017-5-11 工作第二天,开始记录
- Java中单例模式
- python3 中的queue模块
- 输出螺旋数组(难度:1颗星)
- android studio更利于开发设置的属性
- Softmax 函数及其作用(含推导)