Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/josepot/rereducer
Create declarative redux reducers without boilerplate.
https://github.com/josepot/rereducer
boilerplate reducer redux
Last synced: 1 day ago
JSON representation
Create declarative redux reducers without boilerplate.
- Host: GitHub
- URL: https://github.com/josepot/rereducer
- Owner: josepot
- License: mit
- Created: 2017-03-28T21:42:09.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T19:06:59.000Z (almost 2 years ago)
- Last Synced: 2024-04-14T19:51:32.703Z (7 months ago)
- Topics: boilerplate, reducer, redux
- Language: JavaScript
- Homepage:
- Size: 1.62 MB
- Stars: 13
- Watchers: 4
- Forks: 1
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# rereducer
Create declarative redux reducers without boilerplate.
Basic example:
```js
import rereducer from 'rereducer';
const INITIAL_COUNTER_STATE = 0;const counter = rereducer(
INITIAL_COUNTER_STATE,
['INCREASE', x => x + 1],
['DECREASE', x => x - 1]
);counter();
// => 0
counter(5, { type: 'INCREASE' });
// => 6
```What if you want the same transformation for more than one action type?
```js
import rereducer from 'rereducer';
const INITIAL_COUNTER_STATE = 0;const counter = rereducer(
INITIAL_COUNTER_STATE,
['INCREASE', x => x + 1],
['DECREASE', x => x - 1],
[['RESET', 'LOGOUT'], () => INITIAL_COUNTER_STATE]
);counter(100, { type: 'RESET' });
// => 0
counter(100, { type: 'LOGOUT' });
// => 0
```What if you need a more complex condition for a transformation?
```js
import rereducer from 'rereducer';
const INITIAL_COUNTER_STATE = 0;const whenSomethingSpeciallHappens = (state, action) => (
action.type === 'SOMETHING_SPECIAL' &&
action.payload === 'SPECIAL_INDEED' &&
state > 10
);const counter = rereducer(
INITIAL_COUNTER_STATE,
['INCREASE', x => x + 1],
['DECREASE', x => x - 1],
[whenSomethingSpeciallHappens, x => x + 1000]
);counter(11, { type: 'SOMETHING_SPECIAL', payload: 'SPECIAL_INDEED' });
// => 1011
counter(10, { type: 'SOMETHING_SPECIAL', payload: 'SPECIAL_INDEED' });
// => 10
counter(11, { type: 'SOMETHING_SPECIAL' });
// => 11
```### Credits to:
- [@winkerVSbecks](https://github.com/winkerVSbecks): For sharing good ideas and feedback about the API