如何正确处理redux工具包crud中的加载状态

cbeh67ev  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(318)

我想知道在redux工具箱中处理加载状态的最佳方法。
我有这个片段,在这里我可以创建一篇文章,删除一篇文章,获得所有的文章。我认为,对于所有这些操作,我需要处理一个加载状态。因此,当用户等待帖子时,我可以显示一个加载器,如果他创建了帖子,我想向他显示一个加载器,这表明帖子正在创建。
这就是我所拥有的

export const posts = createSlice({
  name: "posts",
  initialState : {posts: [], status: null},
  reducers: {},
  extraReducers: {
    [createPost.pending]: (state, action) => {
      state.status = "loading"; <==== how to do this.
    },
    [createPost.fulfilled]: (state, action) => {
      state.status = "success"; <==== how to do this
    },
    [createPost.rejected]: (state, action) => {
      state.status = "failed"; <==== how to do this
    },

    [getPosts.pending]: (state, action) => {
      state.status = "loading"; <==== how to do this
    },
    [getPosts.fulfilled]: (state, action) => {
      state.status = "success"; <==== how to do this
      state.posts = action.payload.data;
    },
    [getPosts.rejected]: (state, action) => {
      state.status = "failed"; <==== how to do this
    },

    [deletePost.pending]: (state, action) => {
      state.status = "loading"; <==== how to do this
    },
    [deletePost.fulfilled]: (state, action) => {
      state.status = "success"; <==== how to do this
      state.posts = state.posts.filter(el => el.id !== action.payload);
    },
    [deletePost.rejected]: (state, action) => {
      state.status = "failed"; <==== how to do this
    }, 

    [getPostById.pending]: (state, action) => {
      state.status = "loading"; <==== how to do this
    },
    [getPostById.fulfilled]: (state, action) => {
      state.status = "success"; <==== how to do this
      state.post = action.payload;
    },
    [getPostById.rejected]: (state, action) => {
      state.status = "failed"; <==== how to do this
    },
  },
});

我还可以在初始状态下为每个crud操作创建两个以上的键,如(createloadingstate,deleteloadingstate),但我认为这是不对的
我想我可以做这样的东西:

[createPost.pending]: (state, action) => {
  state.status = "creating_post_loading";
},
[createPost.fulfilled]: (state, action) => {
  state.status = "creating_post_success";
},
[createPost.rejected]: (state, action) => {
  state.status = "creating_post_failed";
},

[getPosts.pending]: (state, action) => {
  state.status = "get_posts_loading";
},
[getPosts.fulfilled]: (state, action) => {
  state.status = "get_posts_success";
  state.posts = action.payload.data;
},
[getPosts.rejected]: (state, action) => {
  state.status = "get_posts_failed";
},

但是我必须记住每一个名字,所以这种方法对我来说很糟糕。
我想到的最好的主意就是拥有这个初始状态

const initialState = {
  posts: [],
  loading: null, <==== added
  error: null, <==== added
  selectedPost: {},
};

现在,当我获取所有帖子时,我只写state.loading=true,但在create post中,我将state.loading=false,因为如果有人创建了帖子,我不希望我的帖子在页面上重新加载,但这样,我就不会显示已创建帖子的加载程序状态,因为加载状态为false。
我还可以使用自己的loadingstate为每个操作创建单独的切片,但不知道它是否正确。
我的意思是,在我的react组件中,我检查post是否正在加载,如果是,那么我显示一个加载程序,如果不是,那么我将显示获取的数据,但是如果我的所有crud操作都具有相同的加载状态,那么每当用户添加/删除/更新post时,它将触发所有内容的加载状态。
如果你有github回购协议,我可以在那里找到最佳实践,如果你能寄给我,我将非常高兴。

ifmq2ha2

ifmq2ha21#

您可能需要许多不同的加载指示器,甚至需要多个切片,这两个都可能需要大量的工作。redux toolkit还包含一个名为“rtk查询”的数据获取/api缓存抽象,它自动化了所有这些,因此我建议您阅读关于这方面的文档。
https://redux-toolkit.js.org/rtk-query/overview

相关问题