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 🙏

© 2025 – Pkg Stats / Ryan Hefner

redux-class-decorators

v3.2.4

Published

Simple creation of actions and reducers in Redux.

Downloads

41

Readme

redux-class-decorators

NPM Scrutinizer Code Quality Build Status GitHub Issues Gitter License

Table Of Contents

Installation

npm

npm install redux-class-decorators

yarn

yarn add redux-class-decorators

Overview

Writing reducers can be annoying, it takes time to create actionTypes, and actions, and to put it all into a switch. The benefits of this package is that you don't have to manage a separate actionTypes file. You get to define actions and a reducer in classes and all your types and API calls will live on just some objects. Just a matter of preference.

You haven't to declare or manage string constants. This package meets the standard for stream action objects and will automatically declare string constants for types. Also this package works well with redux-thunk.

If you have any questions, you can see examples of use.

back to top


How To Use

Redux recommends creating constants, action creators and reducers separately. And we try to stick to this rule.

Reducer:

import { ReducerClass } from 'redux-class-decorators'

@ReducerClass('Profile')
class ProfileReducer {
  static initialState = {
    value: null,
    loading: false,
  }

  static startLoading(state, action) {
    return {
      ...state,
      loading: true,
    }
  }

  static finishLoading(state, action) {
    return {
      ...state,
      value: action.payload,
      loading: false,
    }
  }

  static clear(state) {
    return {
      ...state,
      value: null,
      loading: false,
    }
  }
}

Actions:

import { ActionClass } from 'redux-class-decorators'

@ActionClass
class ProfileActionSet {
  static get() {
    return (dispatch, getState) => {
      dispatch({
        type: ProfileReducer.startLoading,
      })

      const profile = { id: 1, name: 'Mike' }

      dispatch({
        type: ProfileReducer.finishLoading,
        payload: profile,
      })
    }
  }

  static clear() {
    return {
      type: ProfileActionSet.clear,
    }
  }
}

Usage:

// Create store
const store = createStore(ProfileReducer.$reducer, null, applyMiddleware(thunk))

// Get dispatch
const dispatch = store.dispatch

dispatch(ProfileActionSet.get())
// Actions:
// { type: 'PROFILE__START_LOADING' }
// { type: 'PROFILE__FINISH_LOADING', payload: { id: 1, name: 'Mike' } }
// state == { value: { id: 1, name: 'Mike' }, loading: false }

dispatch(Something.clear()) // { type: 'PROFILE__CLEAR' }
// state == { value: null, loading: false }

Example of using redux-class-decorators:

back to top


Bonus

PlumbingActionClass allows you to use one class to work with different instances.

Reducer:

import { PlumbingReducerClass } from 'redux-class-decorators'

@PlumbingReducerClass('Banner')
class BannerReducer {
  static $getInitialState() {
    return {
      value: null,
    }
  }

  static setValue(state, action) {
    return {
      ...state,
      value: action.payload,
    }
  }
}

Actions:

import { PlumbingActionClass } from 'redux-class-decorators'

@PlumbingActionClass
class Banner {
  static $getIndex(params) {
    return params.type
  }

  static add(newValue) {
     return {
       type: BannerReducer.setValue,
       payload: newValue,
     }
  }
}

Usage:

// Create store
const store = createStore(BannerReducer.$reducer, null, applyMiddleware(thunk))

// Get dispatch
const dispatch = store.dispatch

dispatch(Banner.add({ type: 'left', text: 'Test1' }))
// { type: 'BANNER__SET_VALUE', payload: 5 }

dispatch(Banner.add({ type: 'right', text: 'Test2' }))
// { type: 'BANNER__SET_VALUE', payload: 10 }

// state == { 'left': { value: 'Test1' }, 'right': { value: 'Test2' } }

back to top


License

MIT