https://github.com/jcoreio/redux-immutable-collections
Reducers and actions for storing collections of documents in Redux state
https://github.com/jcoreio/redux-immutable-collections
Last synced: about 1 year ago
JSON representation
Reducers and actions for storing collections of documents in Redux state
- Host: GitHub
- URL: https://github.com/jcoreio/redux-immutable-collections
- Owner: jcoreio
- License: mit
- Created: 2016-12-12T19:40:12.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-12-21T21:51:16.000Z (over 8 years ago)
- Last Synced: 2025-03-01T22:16:41.989Z (over 1 year ago)
- Language: JavaScript
- Size: 129 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# redux-immutable-collections
[](https://travis-ci.org/jcoreio/redux-immutable-collections)
[](https://coveralls.io/github/jcoreio/redux-immutable-collections?branch=master)
[](https://github.com/semantic-release/semantic-release)
Reducers and actions for storing collections of documents in Immutable.js collections in Redux state.
Designed for Mongo documents, but potentially useful even if you're not using Mongo.
## Usage
```
npm i --save redux-immutable-collections
```
### Keyed collections
```es6
import {reducer as keyedCollectionReducer, actions} from './lib/keyedCollection'
import {createStore} from 'redux'
import {combineReducers} from 'mindfront-redux-utils-immutable'
const USERS = 'USERS.'
const POSTS = 'POSTS.'
// the keyed collection action types are just INSERT, UPDATE, REMOVE, and BATCH,
// unless we specify an action type prefix like so:
const reducer = combineReducers({
users: keyedCollectionReducer({actionTypePrefix: USERS}),
posts: keyedCollectionReducer({actionTypePrefix: POSTS}),
})
const userActions = mapValues(actions(USERS))
const postActions = mapValues(actions(POSTS))
const store = createStore(reducer)
store.dispatch(userActions.insert('28nkdjas9i23kjsdaf', {
username: 'jimbob',
firstName: 'Jim',
lastName: 'Bob',
}))
store.dispatch(userActions.update('28nkdjas9i23kjsdaf', {
email: 'jim@bob.com',
}))
console.log(store.getState())
```
The state will look like this:
```
Map {
"users": Map {
"28nkdjas9i23kjsdaf": Map {
"username": "jimbob",
"firstName": "Jim",
"lastName": "Bob",
"email": "jim@bob.com"
}
},
"posts": undefined
}
```
You can also `remove` documents:
```es6
store.dispatch(userActions.remove('28nkdjas9i23kjsdaf'))
```
If you need to make a lot of changes rapidly, dispatch them in a batch; the reducer will handle them inside a
`withMutations` call, which is much more efficient, and redux subscribers will only be notified once:
```es6
store.dispatch(userActions.batch([
userActions.insert('28nkdjas9i23kjsdaf', {
username: 'jimbob',
firstName: 'Jim',
lastName: 'Bob',
}),
userActions.update('28nkdjas9i23kjsdaf', {
email: 'jim@bob.com',
}),
]))
```
There is a `clear` action as well that will clear the collection.