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-fetch-duck

v1.0.0

Published

Simple and flexible API for creating a Redux module to manage a single fetch request.

Downloads

15

Readme

redux-fetch-duck

Simple and flexible API for creating a redux duck to manage a single fetch request, features loading and error states.

Instalation

It requires redux and thunk middleware.

$ yarn add redux-fetch-duck redux redux-thunk

Example

Simple usage

You can create a single file module in your project

// users.js
import { thunkCreator, withFetch } from 'redux-fetch-duck';

// This function is in charge of fetching the data.
// It must return a promise
const callApi = () => fetch('api.example.com/users');
// Selectors funcions depends on wich library is doing the request. They are optional.
const dataSelector = res => res.data;
const errorSelector = err => err.body.message;

// This functions returns the thunk ready to be disptched.
export const getUsers = thunkCreator('users', callApi, dataSelector, errorSelector)

// creates the duck reducer. 
export default withFetch('users')();

Combine the new duck reducer

// rootReducer.js
import { combineReducers } from 'redux';
import users from './users';

export default combineReducers({
  users,
})

This generate the state initial state

{
  users: {
    data: null,
    loading: false,
    error: null,
  }
}

Finally dispatch the thunk if you want to fetch de data

import React from 'react';
import { connect } from 'react-redux'
import { getUsers } from '../redux/users';

class Container extends React.Component {
  componentWillMount() {
    if (!this.props.users) {
      this.props.dispach(getUsers());
    }
  }
  render() {
    const { loading, users, error } = this.props;
    if (loading) return <div>Loading ...</div>;
    if (error) return <div>Cant fethc data, error: {error}</div>
    return (
      <div>
        {users}
      </div>
    )
  }
}

export default connect(state => ({
  loading: state.users.loading,
  error: state.users.error,
  users: state.users.data,
})(Container);

Extend your reducer

If you need a more complex state for your fetching resource you can import the types and action creators.

// users.js
import { thunkCreator, withFetch, types, actionCreators } from 'redux-fetch-duck';

// return an object with keys request, success, failure
const fetchTypes = types('users');

// this reducer will count the number of calls to the API
const calls = (state = 0, action) => {
  switch (action.type) {
    case fetchTypes.request:
      return state + 1;
    default:
      return state;
  }
};

// pass the reducer to be combined, it calls combineReducer under the hood.
export default withFetch('users')({ calls })

The new initial state will be

{
  users: {
    data: null,
    loading: false,
    error: null,
    calls: 0,
  }
}

API

You can find the api documentation here

Motivation

This module aims to reduce the boilerplate generated when you adopt this common pattern in Redux based apps.

Test

The test suite requires Jest, clone the repo, install the dependencies and then run the tests.

$ yarn
$ yarn test

Contribution

Feel free to create an Issue or even a pull request. You can email me at [email protected]. I'd love to hear your feedback.

License

MIT