0.0.0 • Published 7 years ago

redux-nested-actions v0.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

redux-nested-actions

build status

Namespacing utilities for redux-actions.

Why

People asked for this in redux-actions, but we didn't want to include it in core.

Install

$ npm i --save redux-nested-actions

If you don’t use npm, you may grab the latest UMD build from unpkg (either a development or a production build). The UMD build exports a global called window.ReduxNestedActions if you add it to your page via a <script> tag. We don’t recommend UMD builds for any serious application, as most of the libraries complementary to Redux are only available on npm.

Usage

const { app: { counter: { increment, decrement }, notify, }, login, logout, } = unnestActions( createActions({ 'APP/COUNTER/INCREMENT': amount => ({amount}), 'APP/COUNTER/DECREMENT': amount => ({amount: -amount}), 'APP/NOTIFY': (username, message) => ({message: ${username}: ${message}}), LOGIN: username => ({username}), }, 'LOGOUT') )

expect(increment(1)).to.deep.equal({type: 'APP/COUNTER/INCREMENT', amount: 1}) expect(decrement(1)).to.deep.equal({type: 'APP/COUNTER/DECREMENT', amount: -1 }) expect(notify('yangmillstheory', 'Hello, World!')).to.deep.equal({ type: 'APP/NOTIFY', message: 'yangmillstheory: Hello, World!', }) expect(login('yangmillstheory')).to.deep.equal({ type: 'LOGIN', payload: {username: 'yangmillstheory'}, }) expect(logout()).to.deep.equal({type: 'LOGOUT'})

  </p>
</details>


<details>
  <summary>Using a structured actions map</summary>
  <p>
```js
import {nestActions, unnestActions} from 'redux-nested-actions'
import {createActions} from 'redux-actions'

const actionsMap = {
  APP: {
    COUNTER: {
      INCREMENT: amount => ({amount}),
      DECREMENT: amount => ({amount: -amount}),
    },
    NOTIFY: (username, message) => ({message: `${username}: ${message}`}),
  },
  LOGIN: username => ({username}),
}

const {
  app: {
    counter: { increment, decrement },
    notify,
  },
  login, logout,
} = unnestActions(createActions(nestActions(actionsMap)))

// same expecations as above

License

MIT © 2016, Victor Alvarez