DEV Community

Discussion on: Redux is half of a pattern (1/2)

Collapse
 
helloitsjoe profile image
Joe Boyle

Great post! I went from "why is he calling actions events" to "why weren't actions called events in the first place??" I love the idea of using a status enum instead of a combination of loading, error, data, etc.

One thing... Unless I misunderstand what you're saying about canceling an async thunk request, you can just ignore the success action if the fetch has been canceled:

  switch (action.type) {
    case "FETCHING":
      return { ...state, status: "fetching" };
    case "FETCH_SUCCESS": {
      if (state.status !== "fetching") {
        return state;
      }
      return { ...state, status: "resolved", users: action.payload };
    }
    case "CANCEL":
      return { status: "idle", users: [] };
    default:
      return state;
  }

Here's a working codesandbox: codesandbox.io/s/redux-thunk-cance...

Looking forward to part 2!