如何使用未决承诺来处理Redux工具包中的所有API加载?

发布于 2025-01-27 04:26:19 字数 1344 浏览 2 评论 0原文

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js">
import { createSlice } from "@reduxjs/toolkit";
import { getUser, updateUser } from "./index";
import { getAllData, getData } from "../../logs/store/index";

const manageErrorAndLoading = (state, actionType, error) => {
  state.loading[actionType] = true;
  state.error[actionType] = error;
};

export const loadingSlice = createSlice({
  name: "loading",
  initialState: {
    loading: false
  },
  reducer: {
    toggleLoading: (state) => !state,
  },
  extraReducers: {
    [getUser.pending]: () => true,
    [getUser.fulfilled]: () => false,
    [getUser.rejected]: () => false,
    [updateUser.pending]: () => true,
    [updateUser.fulfilled]: () => false,
    [updateUser.rejected]: () => false,
    [getData.pending]: () => true,
    [getData.fulfilled]: () => false,
    [getData.rejected]: () => false,
  },
});

export const { toggleLoading } = loadingSlice.actions;
export default loadingSlice.reducer;
</script>

我已经使用此方法添加了加载,但效率不高。当一个API加载状态变为真时,加载在所有API上并行运行。

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js">
import { createSlice } from "@reduxjs/toolkit";
import { getUser, updateUser } from "./index";
import { getAllData, getData } from "../../logs/store/index";

const manageErrorAndLoading = (state, actionType, error) => {
  state.loading[actionType] = true;
  state.error[actionType] = error;
};

export const loadingSlice = createSlice({
  name: "loading",
  initialState: {
    loading: false
  },
  reducer: {
    toggleLoading: (state) => !state,
  },
  extraReducers: {
    [getUser.pending]: () => true,
    [getUser.fulfilled]: () => false,
    [getUser.rejected]: () => false,
    [updateUser.pending]: () => true,
    [updateUser.fulfilled]: () => false,
    [updateUser.rejected]: () => false,
    [getData.pending]: () => true,
    [getData.fulfilled]: () => false,
    [getData.rejected]: () => false,
  },
});

export const { toggleLoading } = loadingSlice.actions;
export default loadingSlice.reducer;
</script>

I have used this method to add loading but its not efficient. the loading is running parallel on all api, when one api is loading state gets true.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入画浅相思 2025-02-03 04:26:19

extraReducers(builder) {
builder.addMatcher(isPending, (state, action) => {
  // do what you want when a promise is pending
})}

ISPENDENG也可以用iSrejectediSfulfield替换。

In the RTK matching utilities there is the addMatcher utility, which can catch all the pending, fulfilled and/or rejected promises. For example:

extraReducers(builder) {
builder.addMatcher(isPending, (state, action) => {
  // do what you want when a promise is pending
})}

isPending can be replaced also with isRejected and isFulfilled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文