Giter VIP home page Giter VIP logo

redux-action-utils's Introduction

Redux Action Utils

Factory functions for reducing action creator boilerplate.

npm install --save redux-action-utils

Before

var types = require('./action-types')

function addLesson() {
  return {
    type: types.ADD_LESSON
  }
}

function importLessons(lessons) {
  return {
    type: types.IMPORT_LESSONS,
    lessons
  }
}

function updateLesson(options) {
  return {
    type: types.SELECT_LESSON,
    id: options.id,
    update: options.update
  }
}

module.exports = {addLesson, importLessons, updateLesson}

After

var types = require('./action-types')
var {actionCreator, optionsActionCreator} = require('redux-action-utils')

module.exports = {
  addLesson: actionCreator(types.ADD_LESSON),
  importLessons: actionCreator(types.IMPORT_LESSONS, 'lessons'),
  updateLesson: optionsActionCreator(types.UPDATE_LESSON, 'id', 'update')
}

API

actionCreator(type: String)

Creates an action creator which will create an action object with the given type.

var ac = actionCreator(types.ADD_LESSON)
ac()
// → {type: 'ADD_LESSON'}

actionCreator(type: String, props: Array<String>)

actionCreator(type: String, ...props: String)

Creates an action creator which will create an action object with the given type and use the given property names to pass any positional arguments given to it.

var ac = actionCreator(types.IMPORT_LESSONS, 'lessons')
ac(['lesson 1', 'lesson 2'])
// → {type: 'IMPORT_LESSONS', lessons: ['lesson 1', 'lesson 2']}

optionsActionCreator(type: String)

Creates an action creator which takes a single object argument and adds its properties to the action object.

optionsActionCreator(type: String, props: Array<String>)

optionsActionCreator(type: String, ...props: String)

Creates an action creator which takes a single object argument and adds only the specified properties from it to the action object.

var ac = optionsActionCreator(types.UPDATE_LESSON, 'id', 'update')
ac({
  id: 1
  update: {
    text: '## Lesson 1'
  }
})
/* →
{
  type: 'UPDATE_LESSON',
  id: 1,
  update: {
    text: '## Lesson 1'
  }
}
*/

MIT Licensed

redux-action-utils's People

Contributors

insin avatar

Watchers

Cesar Andreu 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.