Giter Club home page Giter Club logo

reswitch's Introduction

reswitch v0.1.0

Write your reducers with less boilerplate!

Install

npm install --save reswitch

Usage

Through its simplest usage, within a fictional userReducer.js reducer:

yourapp/reducers/usersReducer.js

import reswitch from 'reswitch'

import {
  USERS_GET,
  USERS_GET__SUCCESS,
  USERS_GET__FAILURE
} from 'reducers/users'

function users(state = {
  areLoading: false,
  hasError:   false,
  users:      null
}, action) {
  return reswitch(
    USERS_GET,          {areLoading: true,  hasError: false, users: null},
    USERS_GET__SUCCESS, {areLoading: false, hasError: false, users: action.users},
    USERS_GET__FAILURE, {areLoading: false, hasError: true,  users: null},
  )(state, action.type)
}

export default users

Or you can even hang with functions!

return reswitch(
  USERS_GET,          {areLoading: true,  hasError: false, users: null},
  USERS_GET__SUCCESS, {areLoading: false, hasError: false, users: action.users},
  USERS_GET__FAILURE, () => ({...state, action.error})
)(state, action.type)

Important: In case none of the actions match, then state will be used instead as the default. Maybe you want to use a different fashion, then just leave your reswitch with an odd number of arguments and it will pick the last one as the new default:

return reswitch(
  USERS_GET,          {areLoading: true,  hasError: false, users: null},
  USERS_GET__SUCCESS, {areLoading: false, hasError: false, users: action.users},
  USERS_GET__FAILURE, () => ({...state, action.error}),

  () => ({areLoading: false, hasError: false, users: null, areAdmin: false})
)(state, action.type)

In the above's example we're defining the last argument as a function, but it doesn't need to be. You can use a plain object—or even a complex one!—if you need.

Motivation

I don't like those huge amounts of switches. Although in the official Redux's website they're saying that switches aren't the real boilerplate, I still don't like to use it. It looks like a boilerplate and for me that's reason enough;

License

MIT

reswitch's People

Contributors

chiefgui avatar

Watchers

James Cloos avatar Bruno Quaresma 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.