Skip to content

Support for "Is Loading" State? #107

@iMerica

Description

@iMerica

Hello,

My app has over 75 different types of axios actions using this library and I would like to handle the loading state in a generalized way rather than hundreds of lines of case TYPE.FOO_SUCCESS in my reducers.

For context: I have a single page application with loading spinners at the "page" level and on individual sub components. They all use the same "state.loading" state.

Is there a best practice or pattern here?

I'v tried this:

let loading = false
let requestsHappening = 0;

export const rootReducer = (state = initialState, action) => {
  if (isAxiosRequest(action) && !/(.*)_(SUCCESS|FAIL)/.exec(action.type)) {
    requestsHappening += 1
    loading = true
  } else if (isAxiosRequest(action)) {
    requestsHappening -= 1
    loading = requestsHappening !== 0;
  }
  switch (action.type) {
    // hundreds of lines of Redux boilerplate
    default:
      return {
        ...state,
        loading,
    }
  }
}

But I can't seem to make it work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions