GithubHelp home page GithubHelp logo

create-reducer-redux's Introduction

create-reducer-redux

build status npm version license dependencies status

A redux reducer using the tuple like [ type, handler ] instead of switch statement.

Install

npm install --save @funnyfoo/create-reducer-redux
# or
yarn add @funnyfoo/create-reducer-redux

Usage

import createReducer from '@funnyfoo/create-reducer-redux'

const Types = {
  INCREMENT: 'INCREMENT',
  DECREMENT: 'DECREMENT',
  ADDITION: 'ADDITION'
}

const increment = function() {
  return {
    type: Types.INCREMENT,
  }
}

const decrement = function() {
  return {
    type: Types.DECREMENT,
  }
}

const addX = function(x) {
  return {
    type: Types.ADDITION,
    payload: x
  }
}

const inc = x => x + 1
const dec = x => x - 1

const initialState = 0

const reducer = createReducer([
  [ Types.INCREMENT, inc ],
  [ Types.DECREMENT, dec ],
  [ Types.ADDITION, (state, action) => state + action.payload ],
], initialState)

let state;
state = reducer(state, addX(12))  // => 12
state = reducer(state, decrement())  // => 11
state = reducer(state, { type: 'other' })  // => 11

API

createReducer(pairs, initialState)

Create a redux reducer with the initial state and a list of tuple of action type and handler

Arguments

{Array} pairs: A list of [ ActionType, Handler ], Handler is a function with the two arguments state, action.

{Any} initialState: the initial state for the reducer

Returns

{Function}: returns a function with two arguments state, action

create-reducer-redux's People

Contributors

cesar2535 avatar dependabot[bot] avatar purepennons avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

purepennons

create-reducer-redux's Issues

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.