GithubHelp home page GithubHelp logo

madappgang / action-creator-redux Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 125 KB

A utility function designed to reduce verbosity of redux action creators

Home Page: https://www.npmjs.com/package/@madappgang/action-creator

License: MIT License

JavaScript 100.00%
redux actions action-creator

action-creator-redux's Introduction

Redux action creator

Build Status Coverage Status

It is an unofficial utility function that is designed to reduce verbosity of redux action creators. It is so simple that can fit into a single line of code, but I think it's nice to have on NPM.

Installation

npm i --save @madappgang/action-creator
import actionCreator from '@madappgang/action-creator';

Example

Here we have a common redux action that fetches users.

const types = {
  FETCH_ATTEMPT: 'FETCH_ATTEMPT',
  FETCH_SUCCESS: 'FETCH_SUCCESS',
  FETCH_FAILURE: 'FETCH_FAILURE',
};

const fetchAttempt = () => ({
  type: types.FETCH_ATTEMPT,
});

const fetchSuccess = users => ({
  type: types.FETCH_SUCCESS,
  payload: users,
});

const fetchFailure = err => ({
  type: types.FETCH_FAILURE,
  payload: err,
});

export const fetchUsers = () => async (dispatch) => {
  dispatch(fetchAttempt());

  try {
    ...
    dispatch(fetchSuccess(users));
  } catch (err) {
    dispatch(fetchFailure(err));
  }
};

Actions creators look pretty verbose, and you may have a lot of those in each file. Here's what we can do.

import actionCreator from 'action-creator-redux';

...

const fetchAttempt = actionCreator(types.FETCH_ATTEMPT);
const fetchSuccess = actionCreator(types.FETCH_SUCCESS);
const fetchFailure = actionCreator(types.FETCH_FAILURE);

...

The rest of the code remains the same. This approach implies that you adhere to unified structure of redux action. It means that all the data the action holds is put inside the payload property.

const fetchSuccess = actionCreator(types.FETCH_SUCCESS);
fetchSuccess('This is payload');
// { type: 'FETCH_SUCCESS', payload: 'This is payload' }

There is nothing more to it.

LICENSE

This project is licensed under the MIT License - see the LICENSE file for details.

action-creator-redux's People

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.