Skip to content

Modularize your redux application into independent packs

License

Notifications You must be signed in to change notification settings

iamawebgeek/redux-packed

Repository files navigation

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

About

Modularize your redux application into independent packs

Resources

License

Stars

Watchers

Forks

Packages

No packages published