npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-loading-manager

v1.1.3

Published

Redux reducer and selector for automatically manage loading states.

Downloads

21

Readme

Redux Loading Manager

Redux reducer and selector for automatically manage loading states.

npm version npm bundle size (minified)

Installation

$ npm install redux-loading-manager

or

$ yarn add redux-loading-manager

Motivation

The better part of Redux applications want to control requests performing to show some spinners or loading placeholders. Often, reducers of such applications look like this:

const FETCH_USER_REQUEST = 'FETCH_USER_REQUEST';
const FETCH_USER_SUCCESS = 'FETCH_USER_SUCCESS';
const FETCH_USER_ERROR = 'FETCH_USER_ERROR';

const initialState = {
  isLoading: false,
  user: null,
  error: null
};

function userReducer(state = initialState, action) {
  switch (action.type) {
    case FETCH_USER_REQUEST:
      return { ...state, isLoading: true };

    case FETCH_USER_SUCCESS:
      return { ...state, isLoading: false, user: action.user };

    case FETCH_USER_ERROR:
      return { ...state, isLoading: false, error: action.error };
  }
}

The larger the application, the more handlers:

switch (action.type) {
  case FETCH_USER_REQEST:
    return { ...state, isUserFetching: true };

  case SEND_MESSAGE_REQUEST:
    return { ...state, isMessageSending: true };

  case REMOVE_ARTICLE_REQUEST:
    return { ...state, isArticleRemoving: true };

  // ...
}

Redux loading manager handles all of the requests and automatically stores their loading states, so you no longer need to serve tens of isLoading flags.

Usage

First, put the loading reducer in your root reducer:

import { combineReducers } from 'redux';
import createLoadingReducer from 'redux-loading-manager';

export default combineReducers({
  ...yourReducers,
  loading: createLoadingReducer()
});

Then, use createIsLoadingSelector selector factory to get loading state of any request by passing a request action type as an argument:

import { createIsLoadingSelector } from 'redux-loading-manager';

import types from './types';

export const selectUserLoadingState = createIsLoadingSelector(types.FETCH_USER_REQUEST);

And use it wherever you want!

const mapStateToProps = state => ({
  isUserLoading: selectUserLoadingState(state)
});

Immutable

To use Redux loading manager with Redux-immutable just import the same functions from redux-loading-manager/immutable.

Options

Redux loading manager allows you to pass an options argument to createLoadingReducer function.

| Option | Type | Default value | Description | |----------------|--------|---------------|----------------------------------------------------------------| | requestPostfix | String | _REQUEST | Postfix of request action types. Sets isLoading to true. | | successPostfix | String | _SUCCESS | Postfix of success action types. Sets isLoading to false. | | errorPostfix | String | _ERROR | Postfix of error action types. Also sets isLoading to false. |

If you want to use the loading reducer with the other name, you should pass its name as the second argument of createIsLoadingSelector function:

// rootReducer.js
import { combineReducers } from 'redux';
import createLoadingReducer from 'redux-loading-manager';

export default combineReducers({
  loadingState: createLoadingReducer()
});
// selectors.js
import { createIsLoadingSelector } from 'redux-loading-manager';
import types from './types';

export const selectUserLoadingState = createIsLoadingSelector(types.FETCH_USER_REQUEST, 'loadingState');