Skip to content

Latest commit

 

History

History
166 lines (122 loc) · 3.46 KB

arrayStore.md

File metadata and controls

166 lines (122 loc) · 3.46 KB

Array Store

const {
  reducer,

  set,
  setIn,
  delete,
  insert,
  clear,
  push,
  pop,
  unshift,
  shift
} = arrayStore(uniqueIdentifier, options);

Options

defaultValue

const { set, reducer } = arrayStore('examples/array');
const store = createStore(reducer);
store.getState(); // []
const { set, reducer } = arrayStore('examples/array', { defaultValue: ['hello'] });
const store = createStore(reducer);
store.getState(); // ['hello']
const { set, reducer } = arrayStore('examples/array', { defaultValue: 42 });
const store = createStore(reducer);
store.getState(); // [42]

Action Creators

set

set(value, index)

const { set, reducer } = arrayStore('examples/array', { defaultValue: [0, 1, 2] });
const store = createStore(reducer);

store.getState(); // [0, 1, 2]
store.dispatch(set(1, 0)); // [1, 1, 2]
store.dispatch(set(3, 2)); // [1, 1, 3]
store.dispatch(set(2, 1)); // [1, 2, 3]

setIn

setIn(value, index)

const { set, reducer } = arrayStore('examples/array', {
  defaultValue: [{ a: 0 }, { a: 1 }, { a: 2 }],
});
const store = createStore(reducer);

store.getState(); // [{ a: 0 }, { a: 1 }, { a: 2 }]
store.dispatch(setIn({ a: 'b' }, 0)); // [{ a: 'b' }, { a: 1 }, { a: 2 }]
store.dispatch(setIn({ a: 'b' }, 1)); // [{ a: 'b' }, { a: 'b' }, { a: 2 }]

delete

delete(index)

const { delete: del, reducer } = arrayStore('examples/array', { defaultValue: [0, 1, 2, 3, 4] });
const store = createStore(reducer);

store.getState(); // [0, 1, 2, 3, 4]
store.dispatch(del(0)); // [1, 2, 3, 4]
store.dispatch(del(2)); // [1, 2, 4]

insert

insert(value, index)

const { insert, reducer } = arrayStore('examples/array', { defaultValue: [0, 1, 2, 3, 4] });
const store = createStore(reducer);

store.getState(); // [0, 1, 2, 3, 4]
store.dispatch(insert(1.5, 2)); // [0, 1, 1.5, 2, 3, 4]
store.dispatch(insert(2.5, 4)); // [0, 1, 1.5, 2, 2.5, 3, 4]

clear

clear()

const { clear, reducer } = arrayStore('examples/array', { defaultValue: [0, 1, 2, 3, 4] });
const store = createStore(reducer);

store.getState(); // [0, 1, 2, 3, 4]
store.dispatch(clear()); // []

push

push(value)

const { push, reducer } = arrayStore('examples/array');
const store = createStore(reducer);

store.getState(); // []
store.dispatch(push('hello')); // ['hello']
store.dispatch(push('cruel')); // ['hello', 'cruel']
store.dispatch(push('world')); // ['hello', 'cruel', 'world']
store.dispatch(push(1, 2, 3)); // ['hello', 'cruel', 'world', 1, 2, 3]

pop

pop()

const { pop, reducer } = arrayStore('examples/array', { defaultValue: [0, 1, 2, 3, 4] });
const store = createStore(reducer);

store.getState(); // [0, 1, 2, 3, 4]
store.dispatch(pop()); // [0, 1, 2, 3]
store.dispatch(pop()); // [0, 1, 2]

unshift

unshift(value)

const { unshift, reducer } = arrayStore('examples/array');
const store = createStore(reducer);

store.getState(); // []
store.dispatch(unshift(3)); // [3]
store.dispatch(unshift(2)); // [2, 3]
store.dispatch(unshift(1)); // [1, 2, 3]
store.dispatch(unshift(0)); // [0, 1, 2, 3]
store.dispatch(unshift(-3, -2, -1)); // [-3, -2, -1, 0, 1, 2, 3]

shift

shift()

const { shift, reducer } = arrayStore('examples/array', { defaultValue: [0, 1, 2, 3, 4] });
const store = createStore(reducer);

store.getState(); // [0, 1, 2, 3]
store.dispatch(shift()); // [1, 2, 3]
store.dispatch(shift()); // [2, 3]
store.dispatch(shift()); // [3]