2017-07-31 35 views
1

這是我的傳奇現在:參數傳遞到佐賀調度 - 並獲得參數的處理程序

function* upAsyncSaga() { 
    yield takeEvery(UP_ASYNC, upAsyncHandler); 
} 

這裏是相關的同步材料:

const UP_ASYNC = 'UP_ASYNC'; 
export function upAsync() { 
    return { 
     type: UP_ASYNC 
    } 
} 
function* upAsyncHandler() { 
    yield call(wait, 1000); 
    yield put(up()); 
} 

const UP = 'UP'; 
export function up() { 
    return { 
     type: UP 
    } 
} 

我觸發upAsyncSaga通過這樣做store.dispatch(upAsync())。不過,我想通過一個名爲times的論點。所以我想要做的store.dispatch(upAsync(3)),然後我希望/希望拿到參數的處理這樣的僞代碼:

export function upAsync(times) { // this is the argument "times" 
    return { 
     type: UP_ASYNC 
     times // here it is again 
    } 
} 
function* upAsyncHandler(times) { // i want the argument to get to the handler 
    for (let i=0; i<times; i++) { 
     yield call(wait, 1000); 
     yield put(up()); 
    } 
} 

這可能嗎?

回答

1

當我們派遣任何行動來存儲,我們可以包括的參數(負載)是這樣的:

store.dispatch({ type: UP_ASYNC, payload : { times: 2 }}) 

修改功能:

export function upAsync(type, payload) { 
    const { type, times } = payload 
    return { 
     type: type, 
     times: times 
    } 
} 

我不確定你的全店實施如何,但如果你正在關注REDX-SAGA,上面應該解決你的問題。

編輯:

如果它有效果(我相信)Redux的商店,那麼你可以簡單地執行這樣的操作:

dispatch({type: '<namespace>/upAsyncHandler', payload: {type: UP_ASYNC, times: 2}}) 

這裏,「命名空間」指在終極版命名空間存儲您的處理程序定義爲效果的位置。見下文:

*upAsynHandler(action, { call }) { 
    const { type, times } = action 
    const res = yield call(wait, times) 
} 
+0

謝謝Umesh,但我怎麼能得到'有效載荷'到'upAsyncHandler'? – Blagoh

+0

非常感謝,我現在終於明白了。請原諒我延遲接受您的解決方案。 – Blagoh

相關問題