https://github.com/brianneisler/redux-rereducers
A higher order reducer for performing reduction multiple times until a stable state is found.
https://github.com/brianneisler/redux-rereducers
Last synced: 4 months ago
JSON representation
A higher order reducer for performing reduction multiple times until a stable state is found.
- Host: GitHub
- URL: https://github.com/brianneisler/redux-rereducers
- Owner: brianneisler
- License: mit
- Created: 2016-12-12T06:11:51.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-12-12T08:19:08.000Z (over 8 years ago)
- Last Synced: 2025-02-13T09:17:20.250Z (4 months ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# redux-rereducers
A higher order reducer for performing state calculations after the initial reduction has completed.## Benefits
- Easier to reduce data that depends on multiple state values in the redux store.
- Processes all changes within the same reduction preventing state inconsistencies normally caused by side effects pattern.
- Cleanly decouple reductions based on data that may be affected by multiple actions.
- No need to replicate reductions in several action reducers.## Build Status
[](https://badge.fury.io/js/redux-rereducers)
[](https://travis-ci.org/brianneisler/redux-rereducers)
[](https://nodei.co/npm/redux-rereducers/)## Install
```js
npm install --save redux-rereducers
```## Usage
```js
import { createStore } from 'redux'
import { applyHigherOrders } from 'redux-higher-orders'
import { createRereducerHigherOrder } from 'redux-rereducers'const rereducers = [
(state) => {
...state
},
rereducer2,
rereducerN
]let store = createStore(
reducer,
initialState,
applyHigherOrders(
createRereducerHigherOrder(rereducers)
)
)
```### `createRereducerHigherOrder(...rereducers)`
Creates a higher order reducer that applies the rereducers after the initial primary reduction has been performed.#### Arguments
* `...rereducers` (*arguments*): Functions that conform to the *rereducers API*. Each rereducer receives the `state` only and returns `state`. The rereducer signature is `(state) => state`.#### Returns
* `Function` A higher order reducer that applies the given rereducers.#### Example: Average of Multiple Increment Counters
```js
import { createStore } from 'redux'
import { handleActions } from 'redux-actions'
import { applyHigherOrders } from 'redux-higher-orders'
import { createRereducerHigherOrder } from 'redux-rereducers'const reducer = handleActions({
INCREMENT: (state, action) => ({
counters: {
...state.counters,
[action.payload]: state.counters[action.payload] + 1
}
}),DECREMENT: (state, action) => ({
counters: {
...state.counters,
[action.payload]: state.counters[action.payload] - 1
}
})
}, { counters: {}, average: 0 })const rereducer = (state) => ({
...state,
average: calculateAverage(state.counters)
})const store = createStore(
reducer,
initialState,
applyHigherOrders(
createRereducerHigherOrder([rereducer])
)
)store.dispatch({
type: 'INCREMENT',
payload: 'a'
})store.dispatch({
type: 'INCREMENT',
payload: 'b'
})store.getState() // { counters: { a: 1, b: 1 }, average: 1 }
```