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

@finn-no/redux-actions

v1.1.3

Published

Write actions and reducers for Redux with less boilerplate

Downloads

26

Readme

Redux Actions Build Status

Redux Actions is a tiny library designed to help write Redux action creators and reducers with less boilerplate.

The library is fully tested, has no external dependencies, and weighs in at less than 2 KB uncompressed. It's published in both CommonJS and ESM format, and comes with Typescript definitions.

Getting started

First, install the library using npm or yarn:

npm install --save @finn-no/redux-actions
yarn add @finn-no/redux-actions

Then import the functions in your code:

import { makeAction, makeReducer } from "@finn-no/redux-actions";
const { makeAction, makeReducer } = require("@finn-no/redux-actions");

makeAction

Use makeAction(type, reducer, creator?) to create Redux action creators. This function takes three parameters:

  • type (string) an identifier for this action. This must be a unique value for each action within your app.
  • reducer (function or string) the reducer that will be used to handle actions with this type. Probably the most common way to handle Redux actions is to set a single field in the state to a value provided in the action. For this specific use case, you can provide a string with the name of the state field that this action should affect. An automatic reducer will be provided that sets the value of the specified field in the state to the value of action.payload.
  • creator (optional function) The default action creator creates objects with two fields: type and payload. If you want to create objects with a different format, you will need a custom action creator.

The function returns an action creator which is decorated with the type and reducer of the action. The default action creator accepts one optional argument, the value of which is assigned to the payload field of the action object.

makeReducer

The makeReducer(actions) function takes one parameter: An object where the values are actions made with makeAction. It returns a reducer that handles all the provided actions.

Boilerplate reduction

The primary advantage of this library is reducing the amount of boilerplate required when writing action creators and reducers. The following is an example of the boilerplate that this library removes:

Before

A common structure for apps using Redux is to have one file that defines action type constants, another file with action creator definitions, and a third file containing definitions of reducers. In other words it may be necessary to look at 3 different files in order to fully understand how a given action works.

Regardless of how the code is divided among different files, the structure often looks similar to the following:

/*
 * Action type constants
 */
const MY_ACTION = "MY_ACTION";
const SOME_OTHER_ACTION = "SOME_OTHER_ACTION";

/*
 * Action creators
 */
export function myAction(payload) {
  return {
    type: MY_ACTION,
    payload
  };
}

export function someOtherAction() {
  return {
    type: SOME_OTHER_ACTION
  };
}

/*
 * Reducers
 */
export function reducer(state, action) {
  switch (action.type) {
    case MY_ACTION:
      return { ...state, my_value: action.payload };

    case SOME_OTHER_ACTION:
      return { ...state, some_other_toggle: !state.some_other_toggle };

    default:
      return state;
  }
}

After

With Redux Actions, the definitions of action creators and reducers are kept together, and the need to define action types separately is eliminated:

import { makeAction, makeReducer } from "@finn-no/redux-actions";

const actions = {};

/*
 * Define actions and reducers together
 */
actions.myAction = makeAction("MY_ACTION", "my_value");
actions.someOtherAction = makeAction("SOME_OTHER_ACTION", state => ({
  ...state,
  some_other_toggle: !state.some_other_toggle
}));

export default actions;
export const reducer = makeReducer(actions);

TypeScript

Both makeActions and makeReducer, as well as several other type definitions in this package, are generic types that take one argument; a state type.

import { makeAction, makeReducer, ActionsObject } from "@finn-no/redux-actions";

interface MyState {
    my_value: string;
}

const actions: ActionsObject<MyState> = {};

actions.myAction = makeAction<MyState>("MY_ACTION", "my_value");

export default actions;
export const reducer = makeReducer<MyState>(actions);

About

Redux Actions has been in use in various apps within the travel vertical on FINN.no since 2017. The open source version was released in 2019 under the MIT license.

Copyright © 2019 Schibsted