[英]redux-thunk pass dispatch to another function?
我在當前情況下想使用函數組合:
const funcTwo = (status, response) =>
(dispatch) => {
if (response.error) {
dispatch({ type: TOKEN_FAILURE });
dispatch(notificationShow('ERROR', response.error.message));
} else {
dispatch({ type: TOKEN_SUCCESS, payload: response.id });
}
};
export const funcOne = target =>
(dispatch) => {
dispatch({ type: TOKEN_REQUEST });
Helper.createToken(target, funcTwo);
};
我目前正面臨在funcTwo
無法使用調度的問題,因為它沒有在任何connect()
但我想知道是否有可能以某種方式將調度傳遞給它?
創建兩個本身很笨拙的函數,然后將響應處理邏輯移至第一個。 您的輔助函數應返回一個Promise
以便您可以鏈接成功/錯誤處理程序。
這個想法是,一個thunk可以調度多個動作,而任何一個或全部可能是另一個thunk本身。
偽代碼:
export function requestToken(target){
return (dispatch) => {
dispatch({ type: TOKEN_REQUEST })
Helper.createToken(target)
.then(
response => {
dispatch({
type: TOKEN_REQUEST_SUCCESS,
payload: response.id
})
},
//your createToken failure handler
error => {
dispatch(yourErrorActionCreator(err))
}
)
}
是! 它是。
const funcTwo = (status, response, dispatch) =>
() => {
if (response.error) {
dispatch({ type: TOKEN_FAILURE });
dispatch(notificationShow('ERROR', response.error.message));
} else {
dispatch({ type: TOKEN_SUCCESS, payload: response.id });
}
};
export const funcOne = target =>
(dispatch) => {
dispatch({ type: TOKEN_REQUEST });
Helper.createToken(target, (status, response) => funcTwo(status, response, dispatch ));
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.