[英]How to use Redux Promise Middleware with slices in Redux Toolkit?
我正在使用 Redux Toolkit 的slices
功能。 你可能知道,一個切片為每個創建的reducer
返回一個action
。
我想使用redux-promise-middleware
庫,對於給定的ACTION_TYPE
,它會創建三個可能的新操作: ACTION_TYPE_FETCHING
、 ACTION_TYPE_FULFILLED
和ACTION_TYPE_REJECTED
。 從slices
的角度來看,我該如何處理?
您需要將預期的操作類型添加到createSlice
的extraReducers
部分,例如:
// 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.