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-async-state-reducer

v0.1.2

Published

A reducer and actionCreators to handle the asynchronous state of the application

Downloads

3

Readme

Build Status js-semistandard-style

redux-asyncState-reducer

This is a redux reducer to handle the asynchronous state changes that happen during a request. The idea comes form the Redux documentation on Async Actions

Install

npm install --save redux-async-state-reducer

Usage

Combine the reducer into your root reducer, using combineReducers.

import { createStore, combineReducers } from 'redux'
import asyncState from 'redux-async-state-reducer';

import userReducer from './userReducer';

const rootReducer = combineReducers({
  asyncState,
  userReducer
});

const store = createStore(rootReducer);

ActionCreators

These action creators can be added to the actions of your application. My solution is to export them with the rest of my action creators.

|actionCreator|isFetching|didFail|success| |-------------|------------|---------|---------| |request |true |false |false | |requestFail|false |true |false | |requestSuccess|false |false |true |

My Usage

I use this reducer to inform my application of the state of the asynchronous tasks I am processing. Say I am fetching data from a Database, and want to show a loading component until the results come back.

import React, { Component } from 'react';
import axios from 'axios';

import ProgressBar from 'react-toolbox/lib/progress_bar';
import LongList from './LongList';

class Main extends Component {
  constructor(props) {
    super(props);
    const { actions } = this.props // I keep all my actionCreators under actions
    actions.request();
    // using component state for demo purposes. Explanation below.
    this.state = {
      error: null,
      data: []
    };
    axios.get('path/to/data')
      .then(
        function fullfilled(result) {
          actions.requestSuccess();
          this.setState({
            data: result.data
          });
        },
        function rejection(error) {
          actions.requestFail();
          this.setState({
            error: error
          });
        });
  }

  render() {
      const { asyncState } = this.props;
    return (
      {asyncState.get('isFetching') 
      ? <ProgressBar type='circular' mode='indeterminate' />
      : <LongList data={this.state.data} /> }
    )
  } 
}

I use the other properties didFail & success to help with displaying error as well.

Explanation Instead of using the state of the Component I would dispatch an action with the promise from axios. I would have redux-promise handle this promise by calling then and finally passing the returned data through to the reducers to update the store. However I wanted to show how the request would be made and when to dispatch the actions provided by this module.

License: (MIT)