npm.io
0.0.1 • Published 7 years ago

redux-packed-observable-middleware

Licence
Apache 2.0
Version
0.0.1
Deps
0
Size
2 kB
Vulns
0
Weekly
0
Stars
1

redux-packed

Modularize your redux code into small packs, combine and extract for final usage.

GitHub

Installation

Installing using node package manager. Type the following in your console inside your project directory:

npm install redux-packed --save

With yarn:

yarn add redux-packed

Usage

import { createPacker, updateState, PackData } from 'redux-packed'

const { createPack, combinePacks, extractPack } = createPacker([])

type Store = {
  count: number
}

type Actions = {
  increment: () => ({ type: string })
  decrement: () => ({ type: string })
}

type Packs = [PackData<Store, {}, Actions>]

const pack = createPack<Packs>([
  {
    initial: {
      count: 0,
    },
    actions: {
      INCREMENT: undefined,
      DECREMENT: undefined,
    },
    reducerCreator: (actions, selectors) => ({
      [`${actions.increment}`]: updateState(selectors.count, (value) => value + 1),
      [`${actions.decrement}`]: updateState(selectors.count, (value) => value - 1),
    }),
  },
])

const combinedPack = combinePacks({
  moduleA: pack,
})

const { actions, reducer, selectors } = extractPack(combinedPack)

const store = createStore(reducer)

store.dispatch(actions.increment()) // Dispatch action with type MODULE_A/INCREMENT
console.log(selectors.count(store.getState())) // 1