第一章  认识redux

说的通俗且直白一点呢,就是redux提供了一个store,独立的一个内存区,然后放了一些state,你可以在任何component中访问到state,这些state要更改怎么办呢,store提供了reducer,reducer就是一些函数(纯函数),通过action来对state进行更改,并返回新state。component中如何调用reducer呢,通过dispatch(action),分发action,这样就自动进入对action进行操作的reducer函数了。

第二章 redux 中的reducer

reducer 中有布尔值,可以用来判断

1.reducer 的格式

首先,定义state数据的格式,然后是初始化 state:

一般是用type

export type State = {
username: string,
password: string,
isLoading: boolean,
errors: [],
isLoggedIn: false,
}

初始化state:

const initialState = {
username: "",
password: "",
isLoading: false,
errors: [],
isLoggedIn: false,
}

然后是reducer 不断的增加调用action中的那些function,

export default function (state:State = initialState, action: Action) {
if (action.type === 'SET_USER_NAME') {
return {
...state,
username: action.username,
}
}
if (action.type === 'SET_PASSWORD') {
return {
...state,
password: action.password,
}
}
if (action.type === 'LOGIN_START_LOADING') {
return {
...state,
isLoading: true,
}
}
if (action.type === 'LOGIN_LOADED') {
return {
...state,
isLoading: false,
isLoggedIn: true,
}
}
if (action.type === 'LOGIN_ERROR') {
return {
...state,
isLoading: false,
errors: [...state.errors, action.loginError]
}
}
return state
}

注意:reducer  中只有action  与state 两个参数

第三章.redux  中的action

首先,定义action

export type Action = { type: 'SET_USER_NAME', username: string }
| { type: 'SET_PASSWORD', password: string }
| { type: 'LOGIN_START_LOADING' }
| { type: 'LOGIN_LOADED', loginData: Object }
| { type: 'LOGIN_ERROR', error: Object } export function usernameAction(username) {
return {
type: 'SET_USER_NAME',
username,
};
} export function passwordAction(password) {
return {
type: 'SET_PASSWORD',
password,
}
}

然后页面中通过dispatch  去调用这些function  返回action处理完后最新的数据,并将这些运用dispatch的函数绑定傻瓜component

function mapProps(storeState) {
const {username, password} = storeState;
const isValid = username.length > 6 && password.length > 6;
return {
...storeState,
isValid,
}
} function mapActions(dispatch) {
return { onUsernameChange: (username) => dispatch(usernameAction(username)),
onPasswordChange: (password) => dispatch(passwordAction(password)),
onLoginPress: ()=>dispatch(loginAction), }
} export default connect(mapProps, mapActions)(App);

最后,总结下运行的过程是:

主页通过dispatch引入action获取action处理完后的数据,渲染到页面中,reducer引入相关的action,使用action相关函数获取的state去改变store 里面的函数

action 文件被双向的引入.

												

react native 之 redux的更多相关文章

  1. [RN] React Native 使用 Redux 比较详细和深刻的教程

    React Native 使用 Redux 比较详细和深刻的教程 React Native 使用 Redux https://www.jianshu.com/p/06fc18cef56a http:/ ...

  2. React Native集成Redux框架讲解与应用

    学过React Native的都知道,RN的UI是根据相应组件的state进行render的,而页面又是由大大小小的组件构成,导致每个组件都必须维护自身的一套状态,因此当页面复杂化的时候,管理stat ...

  3. react native 之 redux 使用套路

    redux是什么?他是一个state容器 redux的运作方式是怎样的? 接入方式: 1. npm install 下列内容: npm install --save redux npm install ...

  4. React Native使用Redux总结

    1>npm安装redux: "react-redux": "^5.0.5", "redux": "^3.7.1", ...

  5. [转] 学习React Native必看的几个开源项目

    http://www.lcode.org/study-react-native-opensource-one/ http://gold.xitu.io/entry/575f498c128fe10057 ...

  6. 学习React Native必看的几个开源项目

    学习React native ,分享几个不错的开源项目,相信你学完之后,一定会有所收获.如果还没有了解RN的同学们可以参考手把手教你React Native 实战之开山篇<一> 1.Fac ...

  7. 从React Native到微服务,落地一个全栈解决方案

    Poplar是一个社交主题的内容社区,但自身并不做社区,旨在提供可快速二次开发的开源基础套件.前端基于React Native与Redux构建,后端由Spring Boot.Dubbo.Zookeep ...

  8. react native redux saga增加日志功能

    redux-logger地址:https://github.com/evgenyrodionov/redux-logger 目前Reac native项目中已经使用redux功能,异步中间件使用red ...

  9. 在 React Native 中使用 Redux 架构

    前言 Redux 架构是 Flux 架构的一个变形,相对于 Flux,Redux 的复杂性相对较低,而且最为巧妙的是 React 应用可以看成由一个根组件连接着许多大大小小的组件的应用,Redux 也 ...

随机推荐

  1. charts jupyter notebook 画简单的柱状图

    数据库是mongdb 数据是58同城上发的转手记录 一 为了保证数据安全,对需要进行处理的数据进行拷贝. > db.createCollection('test') { } > show ...

  2. FOJ Problem 2271 X

    Problem 2271 X Accept: 55    Submit: 200Time Limit: 1500 mSec    Memory Limit : 32768 KB Problem Des ...

  3. Python 可变对象与不可变对象

    1. 不可变(immutable):int.字符串(string).float.(数值型number).元组(tuple) 可变(mutable):字典型(dictionary).列表型(list) ...

  4. 洛谷P2483 Bzoj1975 [SDOI2010]魔法猪学院

    题目描述 iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPig对猪世界的世界本原有了很多的了解:众所周知,世界是由元素构成的:元素与 ...

  5. BZOJ 2957 楼房重建 (线段树)

    题目链接  楼房重建 解题思路:我们可以把楼房的最高点的斜率计算出来.那么问题就转化成了实时查询x的个数,满足数列x的左边没有大于等于x的数. 我们可以用线段树维护 设t[i]为如果只看这个区间,可以 ...

  6. BZOJ——2096: [Poi2010]Pilots

    http://www.lydsy.com/JudgeOnline/problem.php?id=2096 Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: ...

  7. Codeforces Round #310 (Div. 2)简洁题解

    A:原来是大水题,我还想去优化.. 结果是abs(num('0')-num('1')); num表示一个符号的个数; B:暴力模拟即可,每次判断是否能构造出答案. C:俄罗斯套娃,套套套,捉鸡的E文. ...

  8. 洛谷P1061 Jam的计数法

    题目描述 Jam是个喜欢标新立异的科学怪人.他不使用阿拉伯数字计数,而是使用小写英文字母计数,他觉得这样做,会使世界更加丰富多彩.在他的计数法中,每个数字的位数都是相同的(使用相同个数的字母),英文字 ...

  9. Spring框架 JdbcTemplate类 @Junit单元测试,可以让方法独立执行 如:@Test

    package cn.zmh.PingCe; import org.junit.Test; import org.springframework.jdbc.core.BeanPropertyRowMa ...

  10. JQuery select 编程时选中原有的值

    js 此为核心代码 $(document).ready(function(){ $("#carTypeId").attr("value",'${carInfo. ...