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-thunk-promise-middleware

v1.0.1

Published

## Why? ##### It removes all the burden of handeling `callbacks` in action creators as well as the problem of maintaing `ACTION_TYPE_SUCCESS` or `ACTION_TYPE_FAILED` .

Downloads

5

Readme

redux-thunk-promise-middleware

Why?

It removes all the burden of handeling callbacks in action creators as well as the problem of maintaing ACTION_TYPE_SUCCESS or ACTION_TYPE_FAILED .

Installation

npm i redux-thunk-promise-middleware

yarn add redux-thunk-promise-middleware

Usage

Defining Store

import { createStore, combineReducers, applyMiddleware } from 'redux';

import ReduxThunkPromiseMiddleware from 'redux-thunk-promise-middleware';

import thunk from 'redux-thunk';
import logger from 'redux-logger';
import RootReducer from './Reducer';

`Note ReduxThunkPromiseMiddleware should always be before thunk middleware`
const store = createStore(RootReducer, applyMiddleware(ReduxThunkPromiseMiddleware, thunk, logger) );

Defining Action Creator

every action creator receives two arguments
1. dispatch
2. start
------ dispatch is used for dispatching actions
------ start is used to track the action .
a. It takes an action type as parameter and dispatch an action with the given type and when the action creator Promise get resolved or reject based on that it automatically fires ${ACTION_TYPE}_SUCCESS or ${ACTION_TYPE}_FAILED
b. The data which will you return from the action creator when the promise is resolved will be dispatched inside a payload of the action with action type ${ACTION_TYPE}_SUCCESS. eg- {type: ${ACTION_TYPE}_SUCCESS , payload: {value} } . As well as it will deliver the same data where this action creatore will be called.

or

If its get rejected then it will automatically dispatch an action like
{type: ${ACTION_TYPE}_FAILED , error: errorData }. As well as it will deliver the same error where this action creatore will be called.

import FETCH_MOVIE_LIST  from '../actionTypes'
export const fetchMovieList = () => (dispatch, start) => {
    start(FETCH_MOVIE_LIST);
    return fetch(url, { method: 'GET'})
           .then((res) => res.json() )
           .then((data) => {
             return {value: data} 
// it will be dispatched as {type: `${ACTION_TYPE}_SUCCESS` , paylaod: {value:data} }
           })
}

Defining Reducer

import FETCH_MOVIE_LIST  from '../actionTypes'

const reducer = (store, action) { 
    
    switch(action.type){
        
        case `${FETCH_MOVIE_LIST}_SUCCESS`: //  rest of code
        case `${FETCH_MOVIE_LIST}_FAILED`: //  rest of code
        default: return store;
    }
    
}

Using the Action creator

import React from 'react'
import {bindActionCreators} from 'redux'
import {connect} from 'react-redux'

import {fetchMovieList} from '../actionCreator';

import Loader from '../Components/Loader';
import MovieList from '../Components/MovieList';

class Home extends React.Component{
    
    constructor(props){
        super(props);
        this.state={
            dataLoading: false
        }
    }
    
   
    componentDidMount(){
        this.loadData();
    }
    
      toogleLoading = () => {
        this.setState((prevState) => ({
            isLoading: !prevState.isLoading
        }))
    }

    
    
    loadData = async () => {
        try{
            this.toogleLoading();
            const result = await this.props.fetchMovieList();
            this.toogleLoading();
        }catch(err){
            this.toogleLoading();
        }
    }
    
    render(){
        const { dataLoading } = this.state;
        const {movieData} = this.props;
        
        return dataLoading ? <Loader /> : <MovieList data={movieData}/>
    }


}
const mapStateToProps = (store) => {
    return {
        movieData: store.movieDatabase
    };
};

const mapDispatchToProps = (dispatch) => bindActionCreators({
    fetchMovieList,
}, dispatch);

export default Home connect(mapStateToProps, mapDispatchToProps)(Home);

Advanced Concepts

Handeling multiple actionCreators

import React from 'react'
import {bindActionCreators} from 'redux'
import {connect} from 'react-redux'

import {fetchMovieList, fetchUserData} from '../actionCreator';

import Loader from '../Components/Loader';
import MovieList from '../Components/MovieList';

class Home extends React.Component{
    
    constructor(props){
        super(props);
        this.state={
            dataLoading: false
        }
    }
    
   
    componentDidMount(){
        this.loadData();
    }
    
      toogleLoading = () => {
        this.setState((prevState) => ({
            isLoading: !prevState.isLoading
        }))
    }

    
    
    loadData = async () => {
        try{
        
         const promises = [this.props.fetchMovieList(), this.props.fetchUserData()];
            this.toogleLoading();
            const result = await Promise.all(promises);
            this.toogleLoading();
        }catch(err){
            this.toogleLoading();
        }
    }
    
    render(){
        const { dataLoading } = this.state;
        const {movieData} = this.props;
        
        return dataLoading ? <Loader /> : <MovieList data={movieData}/>
    }


}
const mapStateToProps = (store) => {
    return {
        movieData: store.movieDatabase
    };
};

const mapDispatchToProps = (dispatch) => bindActionCreators({
    fetchMovieList,
    fetchUserData,
}, dispatch);

export default Home connect(mapStateToProps, mapDispatchToProps)(Home);