1.0.6 • Published 6 years ago

redux-diet v1.0.6

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

redux-diet

Simple Redux Reducer Patterns.

Install

yarn add redux-diet

Include

import * as Diet

Apply

  combineReducers({
    shownModals: Diet.Object('shownModals'),
    shownModalsStatus: Diet.RequestableStatus('shownModals'),
    notes: Diet.Value('notes', ""),
    visited: Diet.Obj('visited', {}),
  })

Invoke

patterns

  • action words are lower case, simple active verbs
  • your fields are whatever you've got
  • dispatch action key is the same as the leaf

Value

Value supports set

  onChange: (notes) => dispatch({type: 'set notes', notes})

Object

  • Object supports set, clear, merge at the object level.
  • Object supports set, clear at the field level.
dispatch({type: 'set visited firstPage', firstPage: new Date()})

Array

  • Array supports set
  • TODO: set element

RequestableStatus

Maintains loaded/error state for other resources

  • Driven by the key of the other resource
  • for key. responds to request ${key}, set ${key}, clear ${key}, set error ${key}
  • can be given a list of other reset action strings that will switch to reset. TODO: Separate this?

Provides two fields: status, error

caveat:

  • set error ${key} adds concern if you have an 'error' at the top level
  • set error ${key} action key is error not ${key}
dispatch({type: 'request shownModals'})
fetch(url)
.then(res => res.json())
.catch(error => {
  dispatch({type: 'set error shownModals', error})
)
.then(shownModals => {
  dispatch({type: 'set shownModals', shownModals})
);
1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago