push(element, index = -1, replace = false)

create.push create(actionType).push Appropriate leaf type: array

Returns a Riduce-standard action to non-mutatively push element to the leaf's state at index index. If replace is true, then element replaces the existing element with that index.

Parameters

Returns

action (object): a Riduce-standard action

Example

import { createStore } from 'redux'
import riduce from 'riduce'

const initialState = {
  foo: ['1', '2', '3'],
  bar: ['1', '2', '3'],
  foobar: ['1', '2', '3']
}

const [reducer, actions] = riduce(initialState)
const store = createStore(reducer)

Providing element

const pushToFoo = actions.foo.create.push
store.dispatch(pushToFoo('4'))
console.log(store.getState().foo) // ['1', '2', '3', '4']

Providing element and index

const pushToBar = actions.bar.create.push
store.dispatch(pushToBar('4', 0)) // push 4 to have index 0
console.log(store.getState().bar) // ['4', '1', '2', '3']

Providing element, index and replace

const pushToFoobar = actions.foobar.create.push
store.dispatch(pushToFoobar('4', 0, true)) // replace 0th element with 4
console.log(store.getState().foobar) // ['4', '2', '3']