簡體   English   中英

如何在 Redux 工具包中將 Redux Promise 中間件與切片一起使用?

[英]How to use Redux Promise Middleware with slices in Redux Toolkit?

我正在使用 Redux Toolkit 的slices功能。 你可能知道,一個切片為每個創建的reducer返回一個action

我想使用redux-promise-middleware庫,對於給定的ACTION_TYPE ,它會創建三個可能的新操作: ACTION_TYPE_FETCHINGACTION_TYPE_FULFILLEDACTION_TYPE_REJECTED slices的角度來看,我該如何處理?

您需要將預期的操作類型添加到createSliceextraReducers部分,例如:

// could use a predefined action constant if desired:
const actionFetching = "ACTION_TYPE_FETCHING"

const usersSlice = createSlice({
  name: "users",
  initialState,
  reducers: {
    // specific case reducers here
  },
  extraReducers: {
    // could use computed key syntax with a separate type constant
    [actionFetching ]: (state, action) => {}
    // or the actual field name directly matching the type string
    ACTION_TYPE_FULFILLED: (state, action) => {}
  }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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