react-native 从简单的事件分发来介绍redux

转载请注明出处:王亟亟的大牛之路

这两天组里来了几个新人,有的用过redux,有的没用过,为了让他们上手或者理解的更透彻,就写了个demo,代码逻辑来源于https://github.com/ninty90/react-native-redux-demo

开篇前先安利
android:https://github.com/ddwhan0123/Useful-Open-Source-Android
react-native:https://github.com/ddwhan0123/Useful-Open-Source-React-Native

源码地址:https://github.com/ddwhan0123/ReduxDemo

演示效果:

理论知识:

http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_one_basic_usages.html

http://www.jianshu.com/p/0e42799be566

http://www.jianshu.com/p/3334467e4b32

理论知识重复炒冷饭等于制造网络垃圾,所以贴上几篇我觉得写得不错的给大家瞅瞅

核心理念:

Store:应用只有一个单一的 Store,State是这个状态集合某一时刻的状态
Action:改变state的载体,也是Store的数据源
Reducer:更新Store的具体操作者

ok,你现在肯定云里雾里的,我们用代码边写边解释

项目结构:

Action相关

MathType

export const ADD_TYPE = 'ADD_TYPE';
export const MINUS_TYPE = 'MINUS_TYPE';

这里是2个常量,"加类型","减类型",我们每种action都有他相对应的类型,可以写在Action里也可以写一个类型对他进行划分,我习惯是拆的越细越好

MathAction

// action类型
import * as types from '../type/MathType';

// 每一个action方法返回一个新的"state"对象,他就是应用当前的状态
export function add(intvalue) {
    console.log('---> MainAction add intvalue ' + intvalue);
    return {
        type: types.ADD_TYPE,
        result: intvalue,
    }
};

export function minus(intvalue) {
    console.log('---> MainAction minus intvalue ' + intvalue);
    return {
        type: types.MINUS_TYPE,
        result: intvalue,
    }
};

Reducer相关

MathReducer

import  * as Type from'../type/MathType';

//初始化
const initState = {
    result: 0
};

export default function mathReducer(state = initState, action = {}) {
    switch (action.type) {
        case Type.ADD_TYPE:
            console.log('---> mathReducer action.type ' + action.type);
            return {
                ...state,
                result: action.result + 10,
            };
            break;
        case Type.MINUS_TYPE:
            console.log('---> mathReducer action.type ' + action.type);
            return {
                ...state,
                result: action.result - 10,
            };
        default:
            return state;
    }
}

肉眼看起来很简单,这里接受两种类型的action一个是➕,一个是➖,每次他都会改变传入参数的值,而且是一定改变,一定会+10或者-10!
reducer只是一个方法,传入什么,返回什么。结果是个恒定值,只要传入参数不变,返回参数一定也不变!

reducers

import  Mathreducer from './Mathreducer';

import  {combineReducers} from 'redux';

export default combineReducers({
    mathStore: Mathreducer,
});

这是一个reducer的大容器,你所有reducer丢一个方法里也不是不能处理,但是性能差加难以维护,所以redux提供combineReducers来帮你整合reducer


Store相关

store是个应用级持有的对象,所以我们把他放到了"根"页面里去做初始化,因为我们之后还会用到异步action,所以还用到redux-thunk的相关内容

import {Provider} from 'react-redux';
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import reducers from'./reducer/reducers';
const middlewares = [thunk];
const createSoreWithMiddleware = applyMiddleware(...middlewares)(createStore);
import  React from 'react';
import  Main from'./Main';

export default class App extends React.PureComponent {
    constructor(props) {
        super(props);

        this.state = {
            store: createSoreWithMiddleware(reducers)
        }
    }

    //前面一些只是对象,方法相关的操作,肉眼可以识别,Provider是让我们决定使用redux的一个原因,它可以让我们操作容器内的组件却不需要手动传递内容
    //想想复杂应用来一个 4层以上的json要你你自己操作的话的工作量吧
    render() {
        return (
            <Provider store={this.state.store}>
                <Main/>
            </Provider>
        )
    }
}


只需要在外面套一层,所有子控件的属性竟在掌握!


页面代码

import React from'react';


import {connect} from 'react-redux';
//加减的两种action
import {add, minus} from './action/MathAction';

import {
    Text,
    View,
    TouchableHighlight,
} from 'react-native';

class Main extends React.Component {
    constructor(props) {
        super(props);
        this.addPress = this.addPress.bind(this);
        this.minusPress = this.minusPress.bind(this);
        //初始值,也可以是外部传入
        this.state = {
            intvalue: 100,
        }
    }

    addPress() {
        console.log('---> Main addPress');
        this.props.dispatch(add(this.state.intvalue));
    }

    minusPress() {
        console.log('---> Main minuPress');
        //dispatch(action) 方法更新 state
        this.props.dispatch(minus(this.state.intvalue));
    }

    //状态变化时会被调用
    shouldComponentUpdate(nextProps, nextState) {
        console.log('---> Main shouldComponentUpdate');
        if (nextProps.result !== this.props.result) {
            this.state.intvalue = nextProps.result;
            console.log('---> Main shouldComponentUpdate this.state.intvalue ' + this.state.intvalue);
            return true;
        }
    }

    render() {
        console.log('---> Main render');
        return (
            <View style={{justifyContent: 'center'}}>
                <TouchableHighlight onPress={this.addPress}>
                    <Text style={{fontSize: 15}}>
                        按我会加
                    </Text>
                </TouchableHighlight>
                <TouchableHighlight style={{marginTop: 30}} onPress={this.minusPress}>
                    <Text style={{fontSize: 15}}>
                        按我会减
                    </Text>
                </TouchableHighlight>
                <Text style={{marginTop: 30, color: '#ffaa11'}}>{this.state.intvalue}</Text>
            </View>
        )
    }
}

function select(store) {
    return {
        result: store.mathStore.result,
    }
}
//connect方法建立数据与状态的关系,达到刷新ui的效果
export  default  connect(select)(Main);

这样这个简单的demo就讲完了,什么?看不懂,我也觉得 这说的是啥啊,过程都没讲清楚,ok 看下console你就明白了!

//首页被加载出来
05-19 20:52:49.094 5992-24741/? I/ReactNativeJS: ---> Main render

//页面点击了 “按我会加”
05-19 20:52:57.746 5992-24741/? I/ReactNativeJS: ---> Main addPress

//action得到了响应获取到了 100(正常的方法调用嘛?继续看!)
05-19 20:52:57.747 5992-24741/? I/ReactNativeJS: ---> MainAction add intvalue 100

//传递到了reducer获取到了触发的类型
05-19 20:52:57.747 5992-24741/? I/ReactNativeJS: ---> mathReducer action.type ADD_TYPE

//页面收到了state改变的讯息,回调被触发
05-19 20:52:57.759 5992-24741/? I/ReactNativeJS: ---> Main shouldComponentUpdate

//新的值是之前的100+reducer的10=110
05-19 20:52:57.759 5992-24741/? I/ReactNativeJS: ---> Main shouldComponentUpdate this.state.intvalue 110

//刷新数据
05-19 20:52:57.759 5992-24741/? I/ReactNativeJS: ---> Main render

//第二次操作,不解释了
05-19 20:53:02.010 5992-24741/? I/ReactNativeJS: ---> Main minuPress
05-19 20:53:02.010 5992-24741/? I/ReactNativeJS: ---> MainAction minus intvalue 110
05-19 20:53:02.010 5992-24741/? I/ReactNativeJS: ---> mathReducer action.type MINUS_TYPE
05-19 20:53:02.015 5992-24741/? I/ReactNativeJS: ---> Main shouldComponentUpdate
05-19 20:53:02.015 5992-24741/? I/ReactNativeJS: ---> Main shouldComponentUpdate this.state.intvalue 100
05-19 20:53:02.015 5992-24741/? I/ReactNativeJS: ---> Main render

action reducer本身看起来平淡无奇,但是在store内轮转使得我们省去了大量setState的人工操作,避免了各种不可描述的render().

但是 redux处理不好会各种多次render页面,之后的文章我会讲一讲异步的action和react-native优化

我是王亟亟!我们下篇见

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容