2017-07-31 72 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

相关问题