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-direct

v1.0.0

Published

Use Redux without string constants

Downloads

15

Readme

Build Status

redux-direct

Use Redux without string constants.

With redux-direct you can use your action creators directly inside the reducers instead of having to define string constants for each action.

Usage

Each action creator created by redux-direct has a toString() method that returns the action type. Combined with ES6 computed properties this allows you to write the following code:

import { createAction, createReducer } from 'redux-direct';

const increment = createAction('INCREMENT');
const decrement = createAction('DECREMENT');

const reducer = createReducer(initialState, {
  [increment]: (state, action) => ({ ...state, counter: state.counter++ }),
  [decrement]: (state, action) => ({ ...state, counter: state.counter-- }),
});

Action Payload

By default, the first argument passed to an action creator is used as payload, the second as meta. Calling increment(2, { foo: 'bar' }) will create the following action:

{
  type: 'INCREMENT',
  payload: 2,
  meta: {
    foo: 'bar',
  },
}

You can dynamically create payloads by providing a payload creator function:

createAction('INCREMENT' by => by || 1);

An additional third argument can be specified to provide predefined meta data. To dynamically create the data you can pass a function that will be called with the same arguments as the payload creator.

Multiple Actions

Usually you want to create multiple actions at once:

import { createActions } from 'redux-direct';
export default createActions('counter', {
  increment: by => by || 1, // payload creator
  decrement: {              // payload and meta
    payload: by => by || 1,
    meta: 'foo',
  },
  reset: false, // use action arguments as payload and meta
});

This will create actions with the types COUNTER_INCREMENT, COUNTER_DECREMENT and COUNTER_RESET.

You can also use a path like __filename as prefix, in which case the file's basename (without the extension) is used.

Action Phases

If you use a middleware like redux-promised, redux-async or redux-promise-middleware that dispatches multiple actions with different suffixes, you can specify a handler for each suffix in your reducer:

createReducer(initialState, {
  [loadUser]: {
    _PENDING: (state, action) => ({ ...state, user: null, error: null }),
    _RESOLVED: (state, action) => ({ ...state, user: action.payload }),
    _REJECTED: (state, action) => ({ ...state, user: null, error: action.payload }),
  }
});

Some middleware implementations like redux-simple-promise dispatch an action without any suffix for the pending state. Since redux-direct uses the same method to build the final action types as it does in createActions() the following definition can be used:

const loadUser = createAction('LOAD_USER');
createReducer(initialState, {
  [loadUser]: {
    _: (state, action) => (/* LOAD_USER */),
    resolved: (state, action) => (/* LOAD_USER_RESOLVED */),
    rejected: (state, action) => (/* LOAD_USER_REJECTED */),
  }
});

API

createAction

createAction(type, [payloadCreator], [metaCreator])

Returns a function that creates FSA compliant actions of the given type. See usage for a basic example.

createActions

createActions([prefix], definition)

Returns an object with methods to create the described actions. See Multiple Actions for an example.

createReducer

createReducer(initialState, handlers)

Returns a reducer function. See usage for a basic example or Action Phases for a more advanced use case.

License

MIT