react-native 從簡(jiǎn)單的事件分發(fā)來介紹redux

轉(zhuǎn)載請(qǐng)注明出處:王亟亟的大牛之路

這兩天組里來了幾個(gè)新人棵里,有的用過redux润文,有的沒用過,為了讓他們上手或者理解的更透徹,就寫了個(gè)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

演示效果:

理論知識(shí):

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

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

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

理論知識(shí)重復(fù)炒冷飯等于制造網(wǎng)絡(luò)垃圾殿怜,所以貼上幾篇我覺得寫得不錯(cuò)的給大家瞅瞅

核心理念:

Store:應(yīng)用只有一個(gè)單一的 Store,State是這個(gè)狀態(tài)集合某一時(shí)刻的狀態(tài)
Action:改變state的載體典蝌,也是Store的數(shù)據(jù)源
Reducer:更新Store的具體操作者

ok,你現(xiàn)在肯定云里霧里的,我們用代碼邊寫邊解釋

項(xiàng)目結(jié)構(gòu):

Action相關(guān)

MathType

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

這里是2個(gè)常量头谜,"加類型","減類型"骏掀,我們每種action都有他相對(duì)應(yīng)的類型,可以寫在Action里也可以寫一個(gè)類型對(duì)他進(jìn)行劃分柱告,我習(xí)慣是拆的越細(xì)越好

MathAction

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

// 每一個(gè)action方法返回一個(gè)新的"state"對(duì)象,他就是應(yīng)用當(dāng)前的狀態(tài)
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相關(guān)

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;
    }
}

肉眼看起來很簡(jiǎn)單截驮,這里接受兩種類型的action一個(gè)是?,一個(gè)是?际度,每次他都會(huì)改變傳入?yún)?shù)的值葵袭,而且是一定改變,一定會(huì)+10或者-10!
reducer只是一個(gè)方法乖菱,傳入什么坡锡,返回什么。結(jié)果是個(gè)恒定值窒所,只要傳入?yún)?shù)不變鹉勒,返回參數(shù)一定也不變!

reducers

import  Mathreducer from './Mathreducer';

import  {combineReducers} from 'redux';

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

這是一個(gè)reducer的大容器吵取,你所有reducer丟一個(gè)方法里也不是不能處理禽额,但是性能差加難以維護(hù),所以redux提供combineReducers來幫你整合reducer


Store相關(guān)

store是個(gè)應(yīng)用級(jí)持有的對(duì)象海渊,所以我們把他放到了"根"頁(yè)面里去做初始化,因?yàn)槲覀冎筮€會(huì)用到異步action,所以還用到redux-thunk的相關(guān)內(nèi)容

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)
        }
    }

    //前面一些只是對(duì)象绵疲,方法相關(guān)的操作,肉眼可以識(shí)別臣疑,Provider是讓我們決定使用redux的一個(gè)原因盔憨,它可以讓我們操作容器內(nèi)的組件卻不需要手動(dòng)傳遞內(nèi)容
    //想想復(fù)雜應(yīng)用來一個(gè) 4層以上的json要你你自己操作的話的工作量吧
    render() {
        return (
            <Provider store={this.state.store}>
                <Main/>
            </Provider>
        )
    }
}


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


頁(yè)面代碼

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));
    }

    //狀態(tài)變化時(shí)會(huì)被調(diào)用
    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}}>
                        按我會(huì)加
                    </Text>
                </TouchableHighlight>
                <TouchableHighlight style={{marginTop: 30}} onPress={this.minusPress}>
                    <Text style={{fontSize: 15}}>
                        按我會(huì)減
                    </Text>
                </TouchableHighlight>
                <Text style={{marginTop: 30, color: '#ffaa11'}}>{this.state.intvalue}</Text>
            </View>
        )
    }
}

function select(store) {
    return {
        result: store.mathStore.result,
    }
}
//connect方法建立數(shù)據(jù)與狀態(tài)的關(guān)系郁岩,達(dá)到刷新ui的效果
export  default  connect(select)(Main);

這樣這個(gè)簡(jiǎn)單的demo就講完了婿奔,什么?看不懂问慎,我也覺得 這說的是啥啊萍摊,過程都沒講清楚,ok 看下console你就明白了如叼!

//首頁(yè)被加載出來
05-19 20:52:49.094 5992-24741/? I/ReactNativeJS: ---> Main render

//頁(yè)面點(diǎn)擊了 “按我會(huì)加”
05-19 20:52:57.746 5992-24741/? I/ReactNativeJS: ---> Main addPress

//action得到了響應(yīng)獲取到了 100(正常的方法調(diào)用嘛冰木?繼續(xù)看!)
05-19 20:52:57.747 5992-24741/? I/ReactNativeJS: ---> MainAction add intvalue 100

//傳遞到了reducer獲取到了觸發(fā)的類型
05-19 20:52:57.747 5992-24741/? I/ReactNativeJS: ---> mathReducer action.type ADD_TYPE

//頁(yè)面收到了state改變的訊息笼恰,回調(diào)被觸發(fā)
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

//刷新數(shù)據(jù)
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本身看起來平淡無(wú)奇,但是在store內(nèi)輪轉(zhuǎn)使得我們省去了大量setState的人工操作社证,避免了各種不可描述的render().

但是 redux處理不好會(huì)各種多次render頁(yè)面逼龟,之后的文章我會(huì)講一講異步的action和react-native優(yōu)化

我是王亟亟!我們下篇見

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末追葡,一起剝皮案震驚了整個(gè)濱河市腺律,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌宜肉,老刑警劉巖匀钧,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異崖飘,居然都是意外死亡榴捡,警方通過查閱死者的電腦和手機(jī)杈女,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門朱浴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人达椰,你說我怎么就攤上這事翰蠢。” “怎么了啰劲?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵梁沧,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我蝇裤,道長(zhǎng)廷支,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任栓辜,我火速辦了婚禮恋拍,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘藕甩。我一直安慰自己施敢,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著僵娃,像睡著了一般概作。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上默怨,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天讯榕,我揣著相機(jī)與錄音,去河邊找鬼匙睹。 笑死瘩扼,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的垃僚。 我是一名探鬼主播集绰,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼谆棺!你這毒婦竟也來了栽燕?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤改淑,失蹤者是張志新(化名)和其女友劉穎碍岔,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體朵夏,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蔼啦,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了仰猖。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片捏肢。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖饥侵,靈堂內(nèi)的尸體忽然破棺而出鸵赫,到底是詐尸還是另有隱情,我是刑警寧澤躏升,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布辩棒,位于F島的核電站,受9級(jí)特大地震影響膨疏,放射性物質(zhì)發(fā)生泄漏一睁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一佃却、第九天 我趴在偏房一處隱蔽的房頂上張望者吁。 院中可真熱鬧,春花似錦双霍、人聲如沸砚偶。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)染坯。三九已至均芽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間单鹿,已是汗流浹背掀宋。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留仲锄,地道東北人劲妙。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像儒喊,于是被迫代替她去往敵國(guó)和親镣奋。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容