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

dense-redux-actions

v0.3.0

Published

A simpler way to handle action types and actions creators for Redux.

Downloads

6

Readme

dense-redux-actions

Small "class based actions creators" to get type safe actions with TypeScript, to benefit from intellisense and to enable unified naming for ease of code navigation. See this blog post for a discussion of why you might use this module.

How to install

yarn add dense-redux-actions

or 

npm install dense-redux-actions -- save

How to use

A simple example

Create a vanilla action creator:

// actions.ts
import { ActionCreator } from 'dense-redux-actions';

export const myActionCreator = new ActionCreator<string>('MY_ACTION_TYPE'); 

Dispatch an action:

import { myActionCreator } from './actions';

const mapDispatchToProps = (dispatch) => {
  return {
    on: (str: string) => dispatch(myActionCreator.create(str)),
  };
}

or from a saga

yield put(myActionCreator.create('some string payload'));    

Consume an action:

switch (action.type) {
  case myActionCreator.type: {
    const payload = myActionCreator.payload(action);
    // do something with payload
  }
}

Example for use with unified naming

Using the action type as name for the action creator makes navigating the code base easier since you don't have to remember what the creator's name is for a given type.

// actions.ts
import { ActionCreator } from 'dense-redux-actions';

interface ResourceType {
  type: string;
  id: string;
}

interface LocationType {
  region: string;
}

export const API_SOME_RESOURCE_REQUEST 
  = new ActionCreator<ResourceType, LocationType>('API_SOME_RESOURCE_REQUEST'); 

Dispatch an action:

const mapDispatchToProps = (dispatch) => {
  return {
    fetch: (resource, region) => 
      dispatch(API_SOME_RESOURCE_REQUEST.create(resource, region))
  }
}

Consume the action:

import { GenericAction } from 'dense-redux-actions'; 
import { API_SOME_RESOURCE_REQUEST } from './actions';

function* processRequests(action: GenericAction) {
  switch (action.type) {
    case API_SOME_RESOURCE_REQUEST.type: {
      const { payload, meta } = API_SOME_RESOURCE_REQUEST.unpack(action);
      ...
    }
  }
}

API

Create a new action creator:

new ActionCreator<PayloadType, MetaType?>(actionType: string)

Ex:

const ACTION = new ActionCreator<string, string>('ACTION');

or 

const ACTION = new ActionCreator<null>('ACTION');

Create a new action:

create(payload: PayloadType, meta?: MetaType): ActionType

Ex:

ACTION.create('test', 'me') // => {type: 'ACTION', _payload: 'test', _meta: 'me'}

Get typed payload from generic action:

payload(action: GenericAction): PayloadType

Ex:

const payload = ACTION.payload(action); // ex. payload: string = 'test'

Get type meta data:

meta(action: GenericAction): MetaType

Ex:

const meta = ACTION.meta(action); // ex. meta: string = 'me'

Unpack both payload and meta data:

unpack(action: GenericAction): { payload: PayloadType, meta: MetaType }

Ex:

const { payload, meta } = ACTION.unpack(action);

Destructuring - if you want to use shorthand for connect ect.

destruct(): { [actionType: string]: (payload: payloadType, meta?: MetaType) }

Ex:

// makes the prop 
// "ACTION_TYPE: (payload: payloadType, meta?: MetaType) => action" available.

const mapDispatchToProps = {
 ...ACTION.destruct(),  
}

Alternatives

If you are look for a framework for handling redux actions instead you may want to checkout:

or