react native 之 redux
第一章 认识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的更多相关文章
- [RN] React Native 使用 Redux 比较详细和深刻的教程
React Native 使用 Redux 比较详细和深刻的教程 React Native 使用 Redux https://www.jianshu.com/p/06fc18cef56a http:/ ...
- React Native集成Redux框架讲解与应用
学过React Native的都知道,RN的UI是根据相应组件的state进行render的,而页面又是由大大小小的组件构成,导致每个组件都必须维护自身的一套状态,因此当页面复杂化的时候,管理stat ...
- react native 之 redux 使用套路
redux是什么?他是一个state容器 redux的运作方式是怎样的? 接入方式: 1. npm install 下列内容: npm install --save redux npm install ...
- React Native使用Redux总结
1>npm安装redux: "react-redux": "^5.0.5", "redux": "^3.7.1", ...
- [转] 学习React Native必看的几个开源项目
http://www.lcode.org/study-react-native-opensource-one/ http://gold.xitu.io/entry/575f498c128fe10057 ...
- 学习React Native必看的几个开源项目
学习React native ,分享几个不错的开源项目,相信你学完之后,一定会有所收获.如果还没有了解RN的同学们可以参考手把手教你React Native 实战之开山篇<一> 1.Fac ...
- 从React Native到微服务,落地一个全栈解决方案
Poplar是一个社交主题的内容社区,但自身并不做社区,旨在提供可快速二次开发的开源基础套件.前端基于React Native与Redux构建,后端由Spring Boot.Dubbo.Zookeep ...
- react native redux saga增加日志功能
redux-logger地址:https://github.com/evgenyrodionov/redux-logger 目前Reac native项目中已经使用redux功能,异步中间件使用red ...
- 在 React Native 中使用 Redux 架构
前言 Redux 架构是 Flux 架构的一个变形,相对于 Flux,Redux 的复杂性相对较低,而且最为巧妙的是 React 应用可以看成由一个根组件连接着许多大大小小的组件的应用,Redux 也 ...
随机推荐
- LightOJ1106 Gone Fishing
Gone Fishing John is going on a fishing trip. He has h hours available, and there are n lakes in the ...
- JConsole手册
一篇Sun官方网站上介绍JConsole使用的文章,前段时间性能测试的时候大概翻译了一下以便学习,今天整理一下发上来,有些地方也不知道怎么翻,就保留了原文,可能还好理解点,呵呵,水平有限,翻的不好,大 ...
- request,response,session,cookie,application
A:request 客服端向服务器端请求 JAVA讲究封装,所以Request也是某个东西的封装,到底是什么东西呢? 按字面意思:请求! 从Http协议说起,当你发一个请求到服务端的时候,你会把一些信 ...
- 聊天程序(基于Socket、Thread)
聊天程序简述 1.目的:主要是为了阐述Socket,以及应用多线程,本文侧重Socket相关网路编程的阐述.如果您对多线程不了解,大家可以看下我的上一篇博文浅解多线程 . 2.功能:此聊天程序功能实现 ...
- css3 nth-child 与 nth-of-type 的区别
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1709 一.深呼吸,直 ...
- springboot主要注解及其作用
1.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...
- Java网络编程之InetAddress和URL
在Java中提供了专门的网络开发程序包---java.net,java的网络编程提供了两种通信协议:TCP(传输控制协议)和UDP(数据报协议). 一.IP(Internet Protocol) 与I ...
- android 长按弹出菜单,复制,粘贴,全选
<!-- 定义基础布局LinearLayout --> <LinearLayout xmlns:android="http://schemas.android.com/ap ...
- 王立平--Gallery:实现图片的左右滑动
<span style="font-size:18px;color:#330033;">package com.main; import android.app.Act ...
- SQL存储过程实例详解
本文用3个题目,从建立数据库到创建存储过程,详细讲解数据库的功能. 题目1 学校图书馆借书信息管理系统建立三个表: 学生信息表:student 字段名称 数据类型 说明 ...