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

v0.6.3

Published

Reduce Redux boilerplate with ease

Downloads

17

Readme

Redux Ease 👌

Reduce Redux boilerplate with ease 🤗

Works great with both JavaScript and TypeScript.

Installation

yarn add redux-ease

or...

npm i redux-ease

Usage example

Let's make a simple counter reducer. It should handle two action types: COUNTER_INCREASE to increase counter value and COUNTER_TO_ZERO to reset counter value.

Initial state looks as simple as:

const initialState = { value: 0 }

At first, lets create the action creator builder and set action types namespace to COUNTER:

// counter-actions.ts

import { getActionCreator } from "redux-ease";

const actionBuilder = getActionCreator('COUNTER')

Then, we have to two options to build and handle action creators:

Main option: No action constants

Let's build our action creators:

// counter-actions.ts

export const counterIncrease = actionBuilder.build('INCREASE', (amount = 1) => ({ amount }))

export const counterToZero = actionBuilder.build('TO_ZERO')

Then, let's create reducer with reducer builder:

// counter-reducer.ts

import { getReducerBuilder } from "redux-ease";

export const counterReducer = getReducerBuilder(initialState)
  .copyState()
  // Type hinting knows that action's payload has `amount` property:
  .handle(counterIncrease, (s, a) => ({ value: s.value + a.payload.amount }))
  .hanlde(counterToZero, () => ({ value: 0 })
  .build()

Thats it! 8 lines and we're done. If you're using IDE which supports TypeScript-based type hinting (WebStorm or VS Code, for example), you can see nice type hints for action payload content.

Extending action creators

If you need to add new logic to existent action creator or change it's arguments list, you can use the extend method of action creator builder. Note that payload of extended action creator should be compatible with parent's payload (type hinting will notify you about this).

// counter-actions.ts

export const counterDoubleIncrease = actionBuilder.extend(counterIncrease, (amount: number = 1) => ({ amount: amount * 2 }))

The extended action creater will receive the same type as parent, so you don't need to make any changes in reducer: if parent action creator is handled in reducer, then all it's chidlrens will be handled too.

Alternative option: With action constants

If you want to define actions constants, Redux Ease also can help you to simplify this task:

// counter-actions.ts

// Type hinting knows that `counterActions` has `INCREASE` and `TO_ZERO` properties.
export const counterActions = actionBuilder.getConstants(['INCREASE', 'TO_ZERO'])

/*
  `counterActions` equals {
    INCREASE: 'COUNTER_INCREASE',
    TO_ZERO: 'COUNTER_TO_ZERO'
  }
*/

Alternatively, if you prefer to create actions manualy you can do this:

// counter-actions.ts

export const INCREASE = actionBuilder.getConst('INCREASE') // equals 'COUNTER_INCREASE'

Then, let's create reducer:

// counter-reducer.ts

export const counterReducer = getReducerBuilder(initialState)
  .copyState()
  .handle(counterActions.INCREASE, (s, a) => ({ value: s.value + a.payload.amount }))
  .hanlde(counterActions.TO_ZERO, () => ({ value: 0 })
  .build()

Unfortunately, type hinting doesn't know action's payload type in this case. However, if you're usng TypeScript, you can specify it manyally:

  // Note that instead of writing payload typings here you can write them in separate .d.ts file. 
  .handle<{ amount: number }>(counterActions.INCREASE, (s, a) => ({ value: s.value + a.payload.amount }))

Usage with immutable library

For example we're gonna use the awesome seamless-immutable library.

counter-actions.ts doesn't changes, so let's look at counter-reducer.ts:

// counter-reducer.ts

import Immutable from 'seamless-immutable'

const initialState = Immutable({ counter: 0 })

export const counterReducer = getReducerBuilder(initialState)
  .copyState()
  .handle(counterIncrease, (s, a) => s.set('value', s.value + a.payload.amount))
  .hanlde(counterToZero, () => s.set('value', 0)
  .build()

That's it!

API

ActionCreatorBuilder

See interface declaration in ./types/IActionCreatorBuilder.d.ts 👀

ReducerBuilder

See interface declaration in ./types/IReducerBuilder.d.ts 👀