簡體   English   中英

我們如何在調度中等待 redux action creator 中的特定值?

[英]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()); } }
我的標志是假的,無法等到標志為真我的意圖是當標志設置為真時,api 調用和數據更新到狀態,我可以在哪里使用狀態信息進行進一步調用,但無法等待超時不是一個好主意在哪里標志值根據 api 調用更新
有什么不同的方法嗎?

首先,如果你在這里粘貼了你的代碼,你應該改變

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM