Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/georapbox/redux-create-reducer
Utility function to express Redux reducers as an object mapping from action types to action handlers.
https://github.com/georapbox/redux-create-reducer
reducers redux utility
Last synced: 3 months ago
JSON representation
Utility function to express Redux reducers as an object mapping from action types to action handlers.
- Host: GitHub
- URL: https://github.com/georapbox/redux-create-reducer
- Owner: georapbox
- License: mit
- Created: 2018-04-21T13:09:26.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-13T23:06:48.000Z (almost 2 years ago)
- Last Synced: 2024-10-10T08:34:41.164Z (3 months ago)
- Topics: reducers, redux, utility
- Language: JavaScript
- Homepage:
- Size: 587 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# redux-create-reducer
Utility function to express Redux reducers as an object mapping from action types to action handlers.
[![npm version](https://img.shields.io/npm/v/@georapbox/redux-create-reducer.svg)](https://www.npmjs.com/package/@georapbox/redux-create-reducer)
[![Build Status](https://travis-ci.com/georapbox/redux-create-reducer.svg?branch=master)](https://travis-ci.com/georapbox/redux-create-reducer)
[![Codecov](https://img.shields.io/codecov/c/github/georapbox/redux-create-reducer/master.svg)](https://codecov.io/gh/georapbox/redux-create-reducer)
[![Dependencies](https://david-dm.org/georapbox/redux-create-reducer.svg)](https://david-dm.org/georapbox/redux-create-reducer)
[![devDependency Status](https://david-dm.org/georapbox/redux-create-reducer/dev-status.svg)](https://david-dm.org/georapbox/redux-create-reducer#info=devDependencies)
[![npm license](https://img.shields.io/npm/l/@georapbox/redux-create-reducer.svg)](https://www.npmjs.com/package/@georapbox/redux-create-reducer)## Install
```sh
$ npm install --save @georapbox/redux-create-reducer
```## API
### createReducer(initialState, handlers [, options={}]) ⇒
function
**Returns**:
function
- A function that returns the next state tree, given the current state tree and the action to handle.| Param | Type | Description |
| --- | --- | --- |
| initialState |\*
| The initial state of the reducer. |
| handlers |Object.<String, Function>
| A plain object mapping action types to action handlers. |
| [options={}]1 |Object
| A plain object for available options. |### 1 Available options
| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `throwForUndefinedHandlers` | `Boolean` | `false` | If set to `true` or any truthy value, it will throw `Error` if `undefined` action handler is encountered (development environment); otherwise it will just print a warning in console. It has no effect in production environment. |## Usage
```js
import createReducer from '@georapbox/redux-create-reducer';const actionTypes = {
ADD_TODO: 'ADD_TODO',
TOGGLE_TODO: 'TOGGLE_TODO'
};const initialState = [];
const handlers = {
[actionTypes.ADD_TODO]: function addTodoHandler(state, action) {
return [
...state,
{
id: action.id,
text: action.text,
completed: false
}
];
},
[actionTypes.TOGGLE_TODO]: function toggleTodoHandler(state, ation) {
return state.map(todo =>
todo.id === action.id
? {...todo, completed: !todo.completed}
: todo
);
}
};export const todosReducer = createReducer(initialState, handlers, {
throwForUndefinedHandlers: true
});
```## License
[The MIT License (MIT)](https://georapbox.mit-license.org/@2018)