1.2.0 • Published 8 years ago

redux-polymorphic v1.2.0

Weekly downloads
5
License
MIT
Repository
github
Last release
8 years ago

Redux Polymorphic

Another attempt to reuse your reduers, inspired from multireducer

Installation

npm i --save redux-polymorphic

How It Works

STEP 1:

import { polymorphicReducer } from 'redux-polymorphic'
// In case you are using Immutable.js, you can:
// import { polymorphicReducer } from 'redux-polymorphic/immutable'
import list from './reduers/list'

const reducer = combineReducers({
  list: polymorphicReducer({
    proposed: list,
    scheduled: list,
    active: list,
    complete: list
  })
})

STEP 2:

import React, { Component, PropTypes } from 'react'
import { bindActionCreators } from 'redux-polymorphic'
import { connect } from 'react-redux'
import { add, remove } from './actions/list'

class TodoListComponent extends Component {
  static propTypes = {
    list: PropTypes.array.isRequired
  }

  render() {
    const { add, list, remove } = this.props
    return (
      <div>
        <button onClick={() => add('New Item')}>Add</button>
        <ul>
          {list.map((item, index) =>
            <li key={index}>
              {item}
              (<button onClick={() => remove(item)}>X</button>)
            </li>)}
        </ul>
      </div>
    )
  }
}

ListComponent = connect(
  (state, { as }) => ({
    list: state.list[as]
  }),
  (dispatch, { as }) => bindActionCreators({ add, remove }, dispatch, as)
)(ListComponent)

export default ListComponent

STEP 3:

render() {
  return (
    <div>
      <h1>Lists</h1>
      <ListComponent as="proposed"/>
      <ListComponent as="scheduled"/>
      <ListComponent as="active"/>
      <ListComponent as="complete"/>
    </div>
  )
}

Manually dispatch

import { polymorphicDispatch } from 'redux-polymorphic'
import { add, remove } from './actions/list'

polymorphicDispatch(dispatch, 'tom')(add)
1.2.0

8 years ago

1.1.0

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

1.0.0-alpha.11

8 years ago

1.0.0-alpha.10

8 years ago

1.0.0-alpha.9

8 years ago

1.0.0-alpha.8

8 years ago

1.0.0-alpha.7

8 years ago

1.0.0-alpha.6

8 years ago

1.0.0-alpha.5

8 years ago

1.0.0-alpha.4

8 years ago

1.0.0-alpha.3

8 years ago

1.0.0-alpha.2

8 years ago

1.0.0-alpha.1

8 years ago

1.0.0-dev

8 years ago