dva學習---effects異步中經過select獲取當前的state

根據 在組件中dispatch一個action的例子中,若是要在effects中對於param數據和當前的state數據進行再出處理,這裏怎麼獲取state呢?採用select,以下:異步

 

  1.  
     
  2.  
    export default {
  3.  
     
  4.  
    namespace: 'example',
  5.  
     
  6.  
    state: {num:1}, //表示當前的example中的state狀態,這裏能夠給初始值,這裏num初始爲1
  7.  
     
  8.  
     
  9.  
    effects: { //這裏是作異步處理的
  10.  
    *addByONe({ param}, { call, put,select }) { //這裏使用select
  11.  
     
  12.  
    const num = yield select(state => state.num) //這裏就獲取到了當前state中的數據num
  13.  
    //方式二: const num = yield select(({num}) =>num)
  14.  
     
  15.  
    //方式三: const num = yield select(_ =>_.num)
  16.  
     
  17.  
    let param1;
  18.  
    param1 = num + param; 這裏就能夠使用num進行操做了
  19.  
     
  20.  
    yield put({
  21.  
    type: 'save',
  22.  
    num:param1
  23.  
    });
  24.  
    }
  25.  
     
  26.  
     
  27.  
    },
  28.  
     
  29.  
    //用來保存更新state值 上面的put方法調用這裏的方法
  30.  
    reducers: {
  31.  
    save(state, action) { //這裏的state是當前總的state,這裏的action包含了上面傳遞的參數和type
  32.  
    return { ...state, ...action.num }; //這裏用ES6語法來更新當前state中num的值
  33.  
    },
  34.  
    },
  35.  
     
  36.  
    };
相關文章
相關標籤/搜索