Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/collaborne/redux-byid
Redux helper to define a "by-id" mapping of reducers
https://github.com/collaborne/redux-byid
Last synced: 1 day ago
JSON representation
Redux helper to define a "by-id" mapping of reducers
- Host: GitHub
- URL: https://github.com/collaborne/redux-byid
- Owner: Collaborne
- License: apache-2.0
- Created: 2018-10-29T13:25:36.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-10T20:39:25.000Z (over 4 years ago)
- Last Synced: 2024-09-21T21:40:39.018Z (about 2 months ago)
- Language: TypeScript
- Size: 58.6 KB
- Stars: 7
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# redux-byid [![Build Status](https://travis-ci.org/Collaborne/redux-byid.svg?branch=master)](https://travis-ci.org/Collaborne/redux-byid)
A helper for redux that allows to distribute an action to one specific part of a map of similar "things".
This [blog post](https://medium.com/collaborne-engineering/redux-pattern-byid-reducers-323a863fa64e?source=friends_link&sk=e48bad30e5d950abf98f366c001f03aa) details the background of this library.
## Usage
```sh
npm install --save redux-byid
```You can then define a "by-id" state:
```js
const { byId } = require('redux-byid');/**
* Reducer for a single item
*/
function itemReducer(state, action) {
switch (action.type) {
case 'item/added':
return {id: action.item_id, data: action.data};
case 'item/updated':
return {id: action.item_id, data: action.updated};
case 'item/deleted':
// Item is deleted, return undefined. byId will drop out the state.
return undefined;
default:
return state;
}
}/**
* Actual reducer, produces a map of item id -> item reducer.
*/
const reducer = byId(action => action.item_id, itemReducer);
```## License
This software is licensed under the Apache 2 license, quoted below.
Copyright 2011-2019 Collaborne B.V.
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.