Skip to content

billbalm/redux-essentials

Repository files navigation

npm node license

redux-essentials

Essential actions and reducers to be used with Redux.

It creates a values entry in the state that can be mutated via 4 actions:

  • ADD VALUE: adds an element to an array at a give path at a given position
  • DEL VALUE: deletes an object property or removes an element from an array
  • MOVE_VALUE: moves an element within an array
  • SET VALUE: sets a value at a give path
  • BATCH: can be used to dispatch multiple actions at once

Action objects are created by calling the methods in the actions module.

The state is treated as proper immutable by this library.

Example

import assert from 'assert';
import { combineReducers, createStore } from 'redux';
import { actions, reducers } from 'redux-essentials';

// creates a brand new store
const store = createStore(combineReducers(reducers));

// set a value deep in the state
store.dispatch(actions.setValue(['cat', 'name'], 'Melinda'));

// throw if cat name isn't 'Melinda'
assert.equal(store.getState().values.cat.name, 'Melinda');

// set a value of type array deep in the state
store.dispatch(actions.setValue(['cat', 'food'], []));

// actions batch
store.dispatch(batch([
  // append some values to the array
  actions.addValue(['cat', 'food'], 'biscuit'),
  actions.addValue(['cat', 'food'], 'meat'),
  // insert a value at a specific position in the array
  actions.addValue(['cat', 'food'], 'fish', 1)),
]));

// throw if food isn't an array exactly like expected
assert.deepEqual(store.getState().values.cat.food, ['biscuit','fish','meat']);

// move meat to the beginning of the array
store.dispatch(actions.moveValue(['cat', 'food'], 2, 0));
// remove 2nd item (biscuit) from the array
store.dispatch(actions.delValue(['cat', 'food'], 1));

// throw if food isn't an array exactly like expected
assert.deepEqual(store.getState().values.cat.food, ['meat','fish']);

Maintainer

billbalm-avatar
Bill Balmant

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published