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 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-actions-magic

v0.1.5

Published

syntax magic for redux actions

Downloads

4

Readme

Redux Actions Magic

Syntax magic for the redux-actions module, meant to remove repetitive typing of action names, that might slightly reduce development time and bug generation.

This module is experimental and may not work perfectly quite yet :)

Install

npm install redux-actions-magic

Note that this module has a peer dependency for redux-actions so you must have that module as a dependecy to your project for it to work.

Usage

Here's what creating a few actions might look like with redux-actions:

// actions.js
import {createAction} from 'redux-actions'

export const ACTION_ONE = 'ACTION_ONE'
export const actionOne = createAction(ACTION_ONE)

export const ACTION_TWO = 'ACTION_TWO'
export const actionTwo = createAction(ACTION_TWO)

Then we can handle them in the reducer like this:

// reducer.js
import {combineReducers} from 'redux'
import {handleAction} from 'redux-actions'
import * as actions from './actions'

export default combineReducers({
  someState: handleActions({
    [actions.ACTION_ONE] => (state, action) => {
      // where the magic happens
    },
    [actions.ACTION_TWO] => (state, action) => {
      // where the magic happens
    }
  })
})

This works fine, but notice we're typing the name of our actions a lot, which gives us a lot of opportunities to make mistakes, especially when we need to rename/refactor our actions as our application grows.

Here's the same functionality with redux-actions-magic:

// actions.js
import {createActions} from 'redux-actions-magic'

// create actions returns an object with two props: types and actions
// types is a plain object that looks something like this: { ACTION_ONE: 'ACTION_ONE', .. }
// action is a plain object that looks something like this: { actionOne: func() { /* action func */ } }
// note that createActions will automatically create camelcased action functions based on the action name
const { types, actions } = createActions([
  'ACTION_ONE',
  'ACTION_TWO',
])

export { types, actions as default }

Notice we only typed our action names once when defining them, and our action function names were generated automatically from the action type.

Here's how we might use our action in a reducer:

// reducer.js
import {combineReducers} from 'redux'
import {handleAction} from 'redux-actions'
import {types as actionTypes} from './actions'

export default combineReducers({
  someState: handleActions({
    [actionTypes.ACTION_ONE] => (state, action) => {
      // where the magic happens
    },
    [actionTypes.ACTION_TWO] => (state, action) => {
      // where the magic happens
    }
  })
})

redux-actions-magic also adds a type property to our action functions, which we can use with our reducer as an alternative to the types property like so:

// reducer.js
import {combineReducers} from 'redux'
import {handleAction} from 'redux-actions'
import actions from './actions'

export default combineReducers({
  someState: handleActions({
    [actions.actionOne.type] => (state, action) => {
      // where the magic happens
    },
    [actions.actionTwo.type] => (state, action) => {
      // where the magic happens
    }
  })
})

What about payload/meta creators?

createActions takes in array of action types to create action functions from. The action types can either be strings for plain actions, or objects with a required type property and optional payload and meta properties for payload/meta creator functions.

That means that this:

createActions([
  'ACTION_ONE',
  'ACTION_TWO',
])

is the exact same as this:

createActions([
  {type: 'ACTION_ONE'},
  {type: 'ACTION_TWO'},
])

That means that creating actions with a payload is as easy as this:

// actions.js
import {createActions} from 'redux-actions-magic'

const { types, actions } = createActions([
  'ACTION_ONE',
  'ACTION_TWO',
  {
    type: 'FANCY_ACTION',
    payload: function() {},
    meta: function() {},
  },
])

export { types, actions as default }

Although putting objects in the array definition works, it can get a little messy, so I prefer this syntax instead:

// actions.js
import {createActions} from 'redux-actions-magic'

// we can put simple actions in an action definitions array
const actionDefs = [
  'ACTION_ONE',
  'ACTION_TWO',
]

// then push fancier actions one at a time for clarity
actionDefs.push({
  type: 'FANCY_ACTION',
  payload: function() {},
  meta: function() {},
})

const { types, actions } = createActions(actionDefs)

export { types, actions as default }

License

The module is released under MIT license