第一章  认识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. Goldbach

    Description: Goldbach's conjecture is one of the oldest and best-known unsolved problems in number t ...

  2. docker使用 命令

    Dockerfile FROM golang:alpine3. AS build-stage WORKDIR /go/src/mypro.exportReport COPY . . RUN go bu ...

  3. hdu1251 字典树trie 模板题

    //字典树模板题.题意:给一个库,每次查询,求以之为前缀的单词数量. #include<iostream> #include<string> #include<vecto ...

  4. 什么是 Linux

    什么是Linux Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络 ...

  5. P2085 最小函数值 洛谷

    https://www.luogu.org/problem/show?pid=2085 题目描述 有n个函数,分别为F1,F2,...,Fn.定义Fi(x)=Ai*x^2+Bi*x+Ci (x∈N*) ...

  6. T1008 选数 codevs

    http://codevs.cn/problem/1008/ 题目描述 Description 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整 ...

  7. 洛谷——P2049 魔术棋子

    P2049 魔术棋子 题目描述 在一个M*N的魔术棋盘中,每个格子中均有一个整数,当棋子走进这个格子中,则此棋子上的数会被乘以此格子中的数.一个棋子从左上角走到右下角,只能向右或向下行动,请问此棋子走 ...

  8. GRYZY #13. 拼不出的数

    拼不出的数 lost.in/.out/.cpp [问题描述] 3 个元素的集合 {5, 1, 2} 的所有子集的和分别是 0, 1, 2, 3, 5, 6, 7, 8.发 现最小的不能由该集合子集拼出 ...

  9. (type interface {}) to type string

    go 语言开发中,经常会在函数中碰到使用 insterface{} 作为接收任意参数,但是我们接收的数据经常是需要做类型转换,由于是初学者,因此,初次转换我是直接就 func New(paramete ...

  10. 【swagger】1.swagger提供开发者文档--简单集成到spring boot中【spring mvc】【spring boot】

    swagger提供开发者文档 ======================================================== 作用:想使用swagger的同学,一定是想用它来做前后台 ...