[英]How to fix “undefined” issue in async call to dispatch in action creator of redux?
[英]How can we await in dispatch for a particular value in redux action creator?
在這里,我將一個標志設置為 true(最初是 flag=false),我希望該標志在另一個減速器中並停止,直到標志為 true
filterReducer.js
const filterReducer = (state = intialState, action) => { switch(action..type){ case actionTypes.FILTER_SUCCESS: return { ...state, filtereData: action.data.messages, flag: true, }; default state; } }
其他動作.js
export const mySolutions = (userId) => { return async (dispatch, getState) => { let flag = await getState().filter.flag; // I am getting flag info from different reducer let data = getState().filter.channelMetaData; console.log("data", data); dispatch(mySolutionStarts()); } }
首先,如果你在這里粘貼了你的代碼,你應該改變
switch(action..type){
// ...
filtereData: action.data.messages,
// ...
}
到
switch(action.type){
// ...
filterData: action.data.messages,
// ...
}
至於在成功的 API 響應上調度操作:我必須假設很多,但我們希望您對 API 調用有一個初始操作。 如果是這樣,您應該在調度第一個操作的同一函數中簡單地調度第二個操作。 就像是:
const myAsyncAction = () => async (dispatch, getState) => {
dispatch({ type: FIRST_ACTION }); // perhaps you want to set the flag here
// async API call here
if (apiRes.status === 200) { // if successful response
dispatch({ type: SECOND_ACTION }) // set flag again
}
}
據我所知,您可以在其中發送任意數量的不同操作。 如果需要,不要忘記導入操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.