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

react-redux-async-action

v1.1.0

Published

Helper methods for dealing with boilerplate of working with react-redux actions that have an eventual outcome

Downloads

15

Readme

react-redux-async-action

Build Status Coverage Status npm NPM

A set of helper functions to reduce the boilerplate of working with redux actions that trigger an async call and end up dispatching a sucess or failure of said action upon completion

Installation:

npm install --save react-redux-async-action
yarn add react-redux-async-action

Motivation

When using redux I often run into this scenario, say we want to fetch some data so we need the action to trigger the call from a component:

export const FETCH_DATA = 'FETCH_DATA';

export const fetchData = (id)=> ({ type: FETCH_DATA, id });

But we also need the sucess and failure variants to dispatch when the call completes:

export const FETCH_DATA_SUCCESS = 'FETCH_DATA_SUCCESS';
export const FETCH_DATA_FAILURE = 'FETCH_DATA_FAILURE';

export const fetchDataSuccess = (data)=> ({ type: FETCH_DATA_SUCCESS, data });
export const fetchDataFailure = (error)=> ({ type: FETCH_DATA_FAILURE, error });

So if we were using thunks, we would need something like this:

async function getDataFromRestApi(id) {
  ...
  return [];
}

export const fetchDataThunk = (id) => (dispatch) => {
  dispatch(fetchData(id));
  getDataFromRestApi(id)
    .then(result=> {
      dispatch(fetchDataSuccess(result));
    })
    .catch(error=> {
      dispatch(fetchDataFailure(error));
    });
};

and your reducer would look something like this:

import { FETCH_DATA, FETCH_DATA_SUCCESS, FETCH_DATA_FAILURE } from './actionTypes';

switch(action.type) {
//...  
  case FETCH_DATA:
    return {...state, fetchingData: true};
  case FETCH_DATA_SUCCESS:
    return {...state, fetchingData: false, data: action.data};
  case FETCH_DATA_FAILURE:
    return {...state, fetchingData: false, fetchError: action.error.message};
//...  
}

This becomes quite repetive when you have different async actions on your application and this aims to alliviate that.

Usage

An important convention

The shape of all your actions should be the same:

{ type, payload, error }
  • type which is the string constant that identifies the action
  • payload can be anything, an object, a string, etc
  • error is optional and will be provided in case its a _FAILURE action

Now you only really need to declare the action:

import { success, failure, asThunk } from 'react-redux-async-action';

export const FETCH_DATA = 'FETCH_DATA';
const fetchData = (id)=> ({ type: FETCH_DATA, payload: id });

and your thunk

export const fetchDataThunk = asThunk(fetchData, (payload)=> getDataFromRestApi(payload.id));

Your reducer would be similar as before, but you can use the sucess and failure helpers on the action type which looks a little bit more semantic.

import { FETCH_DATA } from './actionTypes';
import { success, failure } from 'react-redux-async-action';

switch(action.type) {
//...  
  case FETCH_DATA:
    return {...state, fetchingData: true};
  case success(FETCH_DATA):
    return {...state, fetchingData: false, data: action.data};
  case failure(FETCH_DATA):
    return {...state, fetchingData: false, fetchError: action.error.message};
//...  
}

This convention allows you to handle all failure actions on one place if you want to:

  if(action.type.endsWith('_FAILURE'))
    return {...state, errors: [...state.errors, action.error]};

Available helpers in the package

success

Suffixes the _SUCCESS string to the action's type constant if it's a string or action's type property if it's an action object

function success(action)

|parameter|type|description| |---------|----|-----------| |action|stringobject|The action constant string e.g. 'FETCH_DATA'The action object e.g. { type: 'FETCH_DATA' payload: { id: 42 }}|

failure

Suffixes the _FAILURE string to the action's type constant if it's a string or action's type property if it's an action object.
It also adds an error object to the action representing the error that caused the failure

function failure(action, error)

|parameter|type|description| |---------|----|-----------| |action|stringobject|The action constant string e.g. 'FETCH_DATA'The action object e.g. { type: 'FETCH_DATA' payload: { id: 42 }}| |error|Error|The exception that caused the failure


asThunk

Creates a thunk that calls an async function and dispatches success or failure actions based on the outcome.
It can be customized to do additional logic on either case.

function asThunk(actionCreator, asyncCall, afterSuccess, afterFailure)

|parameter|type|description| |---------|----|-----------| |actionCreator|function(payload)|The action creator function to create a thunk for |asyncCall|function(payload)|The async function to execute when the action is dispatchedits return value will be provided to the SUCCESS action as its payload |afterSuccess|function(action, result, dispatch)|(optional)_ The function to execute after the async call succeeds and SUCCESS action is dispatchedaction: the originally dispatched actionresult: the return value of the asyncCall functiondispatch: the dispatch function in case you want to dispatch additional actions |afterFailure|function(action, error, dispatch)|(optional)_ The function to execute after the async call fails and _FAILURE action is dispatchedaction: the originally dispatched actionerror: the error caught from the asyncCall invocationdispatch: the dispatch function in case you want to dispatch additional actions