react-native redux 與 react-redux 的基本使用

參考原文:
redux http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_two_async_operations.html
react-redux
http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_three_react-redux.html

1 創(chuàng)建 Reducer 根據(jù)不同的action 進行 state的刷新 操作 不做異步操作

創(chuàng)建方法 reducerA.js

const initialState = {
    isRefreshing: false,
    loading: false,
    isLoadMore: false,
    noMore: false,
    articleList: {},
    name:"gy",
    password:"123456"
};

export default function ReducerA(state = initialState,action) {
  switch(action.type){
      case "addA":
          return {
              ...state,
              name: action.name
          };
      case "reduceA":
          return {
              ...state,
              name: action.name
          };
      default:
          return state
  }
}

initialState 存儲的是ReducerA中初始的內(nèi)容
default 必須返回state 不能返回undefind

reducerB.js

const initialState = {
    isRefreshing: false,
    loading: false,
    isLoadMore: false,
    noMore: false,
    articleList: {},
    name:"gy",
    password:"123456"
};

export default function ReducerB(state,action) {
  switch(action.type){
      case "addB":
          return {
              ...state,
              username: action.name
          };
      case "reduceB":
          return {
              ...state,
              username: action.name
          };
      default:
          return initialState
  }
}

initialState 存儲的是ReducerB 中的初始化的state
ReducerA 與 ReducerB中的 initialState 是獨立的存在 兩者沒有關(guān)系

合并兩個ReducerA 與 ReducerB 導(dǎo)出最終的Reducer

import ReducerA from './ReducerA'
import ReducerB from './ReducerB'
import {combineReducers} from 'redux'

export default rootReducer = combineReducers({
    ReducerA,
    ReducerB
})

combineReducers 用來合并多個Reducer 以方便 分塊管理
2 創(chuàng)建store

import {createStore , applyMiddleware} from 'redux'

import rootReducer from './reducer/reducer'

import createSagaMiddleware, { END } from 'redux-saga';

const middlewares = []; /*添加的中間組件*/
const sagaMiddleware = createSagaMiddleware();
middlewares.push(sagaMiddleware)

/*關(guān)聯(lián)中間組件*/
const createStoreWithMiddleware = applyMiddleware(...middlewares)(createStore);

export default function configureStore() {
    const  store = createStoreWithMiddleware(rootReducer)
    store.runSaga = sagaMiddleware.run
    store.close = ()=> store.dispatch(END)
    return store
}

3 在組件中使用 store中的數(shù)據(jù)

import {connect} from 'react-redux'

通過connect 來連接組件的props 與 store中的屬性

export default connect(mapStateToProps,mapDispatchToProps)(Login)

關(guān)聯(lián)屬性的方法 mapStateToProps 是把store中的數(shù)據(jù) 綁定到組件的props中

const mapStateToProps = (state,ownProps)=>{
    console.log("state==="+state.ReducerA.name)
    return {
        name:state.ReducerA.name,
        password:state.ReducerA.password
    }
}

mapDispatchToProps 是組件觸發(fā)action 刷新store中的數(shù)據(jù)

const mapDispatchToProps = {
    add : () => {
        return {
            type:"addA",
            name:"addA"
        }
    }
}

組件調(diào)用代碼:

   render(){
        const {add} = this.props;
        return(
            <View style={styles.container}>
                <TextInput style={styles.input} placeholder={this.props.name}></TextInput>
                <TextInput style={styles.input} placeholder={this.props.password}></TextInput>
                <Button onPress={this.gotoLogin}
                        text="登錄"
                        containerStyle={{alignItems:'center',justifyContent:'center',backgroundColor:'blue',width:100,height:40}}
                        style={{color:'yellow',fontSize:16,textAlign:'center'}}
                        activeOpacity={0.8}></Button>
                <Button onPress={add}
                        text="登錄"
                        containerStyle={{alignItems:'center',justifyContent:'center',backgroundColor:'blue',width:100,height:40}}
                        style={{color:'yellow',fontSize:16,textAlign:'center'}}
                        activeOpacity={0.8}></Button>
            </View>
        )
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 從上周開始一直在看 Redux 相關(guān)的文檔和資料,一開始一連幾天都沒有進展,只能一邊看文檔一邊默默 debug,到...
    aJIEw閱讀 2,651評論 2 11
  • 一、CMS管理系統(tǒng)功能 CMS是ContentManagementSystem的縮寫,意為"內(nèi)容管理系統(tǒng)"。 CM...
    默默先生Alec閱讀 5,615評論 0 7
  • 技術(shù)棧: react + redux + webpack + react-router + ES6/7/8 + i...
    黃昏少年閱讀 3,131評論 0 19
  • 一、參考文章,建議按順序閱讀 1、react-native中使用redux的原理分析及demohttps://ww...
    sy隨緣閱讀 759評論 0 1
  • 今夕何夕。 周末參加婚禮,然后回了一趟安慶表姐家里。周二回來晚上去查看班上斷腿的學(xué)生,第二天又和領(lǐng)導(dǎo)匯報,然后陪領(lǐng)...
    茉莉莫閱讀 111評論 0 1

友情鏈接更多精彩內(nèi)容