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-action-binder

v1.0.5

Published

A helper to easily and efficiently bind and expose redux actions

Downloads

14

Readme

redux-action-binder

A helper to easily and efficiently expose bound action creators.

Installation

npm install redux-action-binder

Usage

This was designed with "Ducks reducer bundles" in mind but regardless, it will bind all named functions located in a module. As an example, assuming a structure similar to this:

redux/
  reducer.js  // returns the combined reducer
  actions.js  // returns the bound actions (see below)
  modules/
    auth.js   // reducer modules exporting action constants, actions and module reducer
    login.js
    posts.js
// actions.js
import actionBinder from 'redux-action-binder';

// extract actions from all modules
import * as login from './login';
import * as auth from './auth';
import * as posts from './posts';

actionBinder.bindActions({ login, auth, posts });
const getBoundActions = actionBinder.getBoundActions;
export { getBoundActions };

In the PostsContainer class:

import { getBoundActions } from './redux/actions';

function mapStateToProps(state) {
  return {
    posts: state.posts
  };
}

function mapDispatchToProps(dispatch) {
  return getBoundActions(dispatch).posts();
}

// or to assign actions from multiple modules to the container...

import objectAssign from 'object-assign';

function mapDispatchToProps(dispatch) {
  return objectAssign({},
    getBoundActions(dispatch).login(),
    getBoundActions(dispatch).posts()
  );  
}

Note that getBoundActions returns an object who's properties are functions. Calling posts() the first time will invoke bindActionCreators on all post actions and subsequent calls will return the already bound actions. Actions are bound only once per each module.

IMPORTANT: By default, the current implementation imports all named functions found in the provided modules. In production builds this will not work if mangling is enabled. You must disable name mangling to use this, for example if using Webpack you can configure the Uglify plugin like this:

// Minify JS
new webpack.optimize.UglifyJsPlugin({
  mangle: false
})

If you have name mangling enabled you must mark all of the action creator functions using the static string "@action name;", for example:

export function loginSubmit() {
  "@action loginSubmit";
  return {type: LOGIN_SUBMIT, user, pass}
}

The name following the @action marker will be used as the property name of the function. This way, even if the function names are mangled this will still work:

  getBoundActions(dispatch).login().loginSubmit(user, pass)

To enable this feature you must configure the action binder prior to calling bindActions:

actionBinder.config({useAnnotations: true});

IMPORTANT NOTE: See warning about testing below.

What gets bound

If not using annotations the actionBinder currently imports all named functions from the modules provided. If the modules include other functions you can tell the actionBinder to ignore them by providing a second argument:

actionBinder.bindActions(...modules, ['func1', 'func2']);

In this case it will ignore functions func1 and func2.

Warning about running tests

If you are running tests with a beforeEach that resets the store, don't forget that the action binder caches the bound functions. For this specific use case, you will need to reset it and rebind the actions. For example, here is the redux/actions.js that exposes the bound methods as well as the reset function:

import actionBinder from 'redux-action-binder';
import * as login from './modules/login';
import * as auth from './modules/auth';
import * as posts   from './modules/posts';

actionBinder.bindActions({login, auth, posts});
const getBoundActions = actionBinder.getBoundActions;

const resetActionBinder = () => {
  actionBinder.reset();
  actionBinder.bindActions({login, auth, posts});
};

export { getBoundActions, resetActionBinder };

Call the resetActionBinder method each time you reconfigure the store for your tests or the actions will be executing against a different store instance.