第一章  认识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. 转载:c++打印日志文件的一个模板

    转载地址:http://blog.csdn.net/huangyifei_1111/article/details/52134914 NetDataLog.h #ifndef NETDATALOG_H ...

  2. spring mvc 单元测试示例

    import java.awt.print.Printable; import java.io.IOException; import javax.servlet.http.HttpServletRe ...

  3. readonly和disabled的异同

    好久木有写博客了,今来逛逛 话说今天搞form表单的时候,主管让俺把手机号设成只读的.当时我就...咳咳,然后我就问了下万能的百度君,果断还是有解决方法的嘛,那么,今就谈谈readonly和disab ...

  4. noj 2069 赵信的往事 [yy题 无限gcd]

    njczy2010 2069 Accepted 31MS   224K 1351Byte G++ 2014-11-13 13:32:56.0 坑爹的无限gcd,,,尼玛想好久,原来要x对y算一次,y再 ...

  5. Day 2 操作系统基础

    课前复习新知识 RAID(Redundant Arrays of Independent Disks)独立冗余磁盘阵列 定义:加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵 ...

  6. qu de hanzi 首字母

    Function hztopy(hzpy As String) As StringDim hzstring As String, pystring As StringDim hzpysum As In ...

  7. raspi串口、python串口模块pyserial

    一.安装 1.下载软件包pyserial-2.7.tar.gz   网址:https://pypi.python.org/pypi/pyserial 2.8uftp上传至/usr/local/src/ ...

  8. HNOI_2002 营业额统计(Splay)

    此题可以用STL的multiset解决,也可以手打一棵伸展树(Splay)来求前驱与后驱. 使用multiset: #include<iostream> #include<set&g ...

  9. Linux C多线程编程-线程互斥

    Linux下的多线程编程需要注意的是程序需要包含头文件pthread.h,在生成可执行文件的时候需要链接库libpthread.a或者libpthread.so. 线程创建函数: pthread_cr ...

  10. 【kotlin】long转化为date类型 或者date字符串

    1.方法体中的 package org.joda.time.DateTime(long类型) fun Long?.toDateTime() = if (null != this) DateTime(t ...