React Native redux理解

React-redux提供Provider和connect兩個接口鏈接

  • Provider組件在應(yīng)用最外層,傳入store即可,只用一次
  • Connect負責從外部獲取組件需要的參數(shù)
  • Connect可以用裝飾器的方式來寫
  • 你要state什么屬性放到props里
  • 你要什么方法拓萌,放到props里揍移,自動dispatch
  • 兩個reducers,每個reducers都有一個state
  • 復(fù)雜redux應(yīng)用绎秒,多個reducer,用combineReducers合并
  • 合并所有reducer搔涝,并且返回
  • 把store.dispatch方法傳遞給組件,內(nèi)部可以調(diào)用修改狀態(tài)
  • connect負責鏈接組件曾雕,給到redux里的數(shù)據(jù)放到組件的屬性里
1.負責接受一個組件奴烙,把state里的一些數(shù)據(jù)放進去,返回一個組件
2.數(shù)據(jù)變化的時候剖张,能夠通知組件
  • Provider,把store放到context里切诀,所有的子元素可以直接取到store

redux理論抽象,寫一個例子:寫一個需要登錄的例子修械,有兩個reducer趾牧。

0.gif

1.兩個reducer:一個reducer用來控制登錄態(tài)检盼,一個用來管理機槍數(shù)量肯污。通過combineReducers()合并到一個reducer中。

  • authReducer:控制登錄態(tài),有登錄和注銷兩個action吨枉。
const LOGIN = 'LOGIN'
const LOGOUT = 'LOGOUT'

export function auth(state={isAuth:false,user:'李云龍'},action){
    switch(action.type){
        case LOGIN:
            return {...state,isAuth:true}
        case LOGOUT:
            return {...state,isAuth:false}
        default:
            return state;
    }
}

export function login(){
    return {type:LOGIN}
}
export function logout(){
    return {type:LOGOUT}
}
export default auth;
  • gunReducer:控制機槍數(shù)量,有增加機槍蹦渣,減少機槍,過兩天再加(異步)三個action貌亭。
const ADD_GUN = '加機關(guān)槍'
const REMOVE_GUN = '減機關(guān)槍'
function counter (state=10,action){
  switch(action.type){
      case ADD_GUN:
      return state + 1;
      case REMOVE_GUN:
      return state - 1;
      default:
          return 10;
  }
}

export function addGun(){
    return {type:ADD_GUN}
}
export function removeGun(){
    return {type:REMOVE_GUN}
}
export function addGunAsync(){
    return dispatch => {
        setTimeout(
            ()=>{dispatch(addGun())
        },2000)
    }
}
export default counter;
  • 合并兩個reducer:authReducer和gunReducer,可能根據(jù)業(yè)務(wù)需要會有多個reducer柬唯。
import counter from '../reducer/gunReducer';
import auth from '../reducer/authReducer';
import {combineReducers} from 'redux';
export default combineReducers({counter,auth});

2.reducer傳入store中,有異步會用到中間件。

import {createStore,applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import reducer from '../reducer/reducer'

export default () => {
    //根據(jù) reducer 初始化 store
    const store = createStore(reducer,applyMiddleware(thunk));
    return store;
}

3.App中引入store,第一層包裝Provider圃庭。

import React, { Component } from 'react';
//引用外部文件
import {Provider} from 'react-redux';
import {createAppContainer,createStackNavigator} from 'react-navigation';
import Main from './app/containers/Main';
import LoginScreen from './app/containers/LoginScreen';
import contextScreen from './app/containers/ContextScreen';
import ContextToPropScreen from './app/containers/ContextToPropScreen';
import configureStore from './app/redux/store/store';
const store = configureStore();
let RootStack = createStackNavigator({
  mainScreen:Main,
  loginScreen:LoginScreen,
  contextToPropScreen:ContextToPropScreen,
  contextScreen:contextScreen
});
let Navigation = createAppContainer(RootStack);
//調(diào)用 store 文件中的 mainReducer常量中保存的方法
export default class App extends Component {
  render() {
    return (//第一層包裝锄奢,為了讓 main 能夠拿到 store
      <Provider store={store}>
      <Navigation />
      </Provider>
      )
  };
}

4.引入reducer中的action失晴,在要用到redux中保存的action和state的頁面獲取state和action,鏈接器進行鏈接拘央。鏈接上之后state和action均以this.props.num,this.props.isAuth,this.props.login,this.props.logout方式調(diào)用涂屁。

import { connect } from 'react-redux';
import { addGun, removeGun, addGunAsync } from '../redux/reducer/gunReducer';
import {login,logout} from '../redux/reducer/authReducer'
// 獲取 state 變化
const mapStateToProps = (state) => {
    return {
        num: state.counter,//counter這個reducer中只有一個變量,可以直接賦值灰伟。
        isAuth:state.auth.isAuth//redux中有兩個reducer,auth這個reducer中又包含兩個state:isAuth和user拆又。
    }
}
// // 發(fā)送行為
const mapDispatchToProps = ({
    addGun: addGun,
    removeGun: removeGun,
    addGunAsync: addGunAsync,
    login:login,
    logout:logout
});
// const actionCreators = { addGun, removeGun, addGunAsync,login,logout}
// 進行第二層包裝,生成的新組件擁有 接受和發(fā)送 數(shù)據(jù)的能力
Main = connect(mapStateToProps, mapDispatchToProps)(Main)
export default Main

5.render()里面判斷是否登錄,若登錄了栏账,跳到獨立團頁面帖族,未登錄跳登錄頁。

    render() {
        return(
         <View style={{
            flex: 1,
            justifyContent: 'center',
            alignItems: 'center',
            backgroundColor: '#F5FCFF',
        }}>
           {this.props.isAuth ? this._independentView():this._loginView()}
             </View>
        );
    }

6.點擊申請武器則調(diào)用this.props.addGun挡爵,進入gunReducer響應(yīng)對應(yīng)key:ADD_GUN,默認state為10竖般,state則加1。上交武器則調(diào)用this.props.removeGun,進入gunReducer響應(yīng)對應(yīng)key:REMOVE_GUN,state則減1,點擊拖兩天再給則調(diào)用this.props.this.props.addGunAsync,模擬異步延時兩秒執(zhí)行addGun(),兩秒后state加1;點擊登錄茶鹃,進入authReducer響應(yīng)對應(yīng)key:LOGIN,isAuth為true捻激,點擊注銷,進入authReducer響應(yīng)對應(yīng)key:LOGOUT,isAuth為false前计。

_independentView() {
        return (
        <View>
            <Text style={{
                fontSize: 36,
                textAlign: 'center',
                margin: 10,
            }}>
                獨立團
        </Text>
            <Text style={{ fontSize: 24 }}>
                現(xiàn)在有機槍{this.props.num}把
        </Text>
            <TouchableOpacity style={{ marginTop: 36 }} onPress={this.props.addGun}>
                <Text style={{ fontSize: 24,alignSelf:'center',marginTop:8}}>申請武器</Text>
            </TouchableOpacity>
            <TouchableOpacity onPress={this.props.removeGun}>
                <Text style={{ fontSize: 24,alignSelf:'center',marginTop:8}}>上交武器</Text>
            </TouchableOpacity>
            <TouchableOpacity onPress={this.props.addGunAsync}>
                <Text style={{ fontSize: 24,alignSelf:'center',marginTop:8 }}>拖兩天再給武器</Text>
            </TouchableOpacity>
            <TouchableOpacity style={{ marginTop: 36 }} onPress={this.props.logout}>
                <Text style={{ fontSize: 24,alignSelf:'center' }}>注銷</Text>
            </TouchableOpacity>
        </View>
        )
    }
    _loginView() {
        return(
        <View>
            <Text>你沒有權(quán)限胞谭,需要登錄才能看</Text>
            <TouchableOpacity onPress={
                    this.props.login
                }>
                <Text style={{ fontSize: 24,alignSelf:'center',marginTop:36}}>登錄</Text>
            </TouchableOpacity>
        </View>
        )
    }

Demo。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末男杈,一起剝皮案震驚了整個濱河市丈屹,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌伶棒,老刑警劉巖旺垒,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異肤无,居然都是意外死亡先蒋,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門宛渐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來竞漾,“玉大人,你說我怎么就攤上這事窥翩∫邓辏” “怎么了?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵寇蚊,是天一觀的道長笔时。 經(jīng)常有香客問我,道長仗岸,這世上最難降的妖魔是什么允耿? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任借笙,我火速辦了婚禮,結(jié)果婚禮上较锡,老公的妹妹穿的比我還像新娘提澎。我一直安慰自己,他們只是感情好念链,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布盼忌。 她就那樣靜靜地躺著,像睡著了一般掂墓。 火紅的嫁衣襯著肌膚如雪谦纱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天君编,我揣著相機與錄音跨嘉,去河邊找鬼。 笑死吃嘿,一個胖子當著我的面吹牛祠乃,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播兑燥,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼亮瓷,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了降瞳?” 一聲冷哼從身側(cè)響起嘱支,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎挣饥,沒想到半個月后除师,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡扔枫,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年汛聚,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖裙品,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情瞄桨,我是刑警寧澤,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布讶踪,位于F島的核電站,受9級特大地震影響泊交,放射性物質(zhì)發(fā)生泄漏乳讥。R本人自食惡果不足惜柱查,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望云石。 院中可真熱鬧唉工,春花似錦、人聲如沸汹忠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽宽菜。三九已至谣膳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間铅乡,已是汗流浹背继谚。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留阵幸,地道東北人花履。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像挚赊,于是被迫代替她去往敵國和親诡壁。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354

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