push(element, [index = -1], [replace = false])
create.push
create(actionType).push
Appropriate leaf type: array
Returns an (action) object that the reduxLeaves reducer uses 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
element
(any): the element to insert to the leaf's stateindex
(integer, optional): the index of the array whereelement
should be insertedreplace
(boolean, optional): whether or notelement
should replace the currentindex
th element
Returns
action
(object): an object to dispatch to the store
Example
import { createStore } from 'redux'
import reduxLeaves from 'reduxLeaves'
const initialState = {
foo: [1, 2, 3],
bar: [1, 2, 3],
foobar: [1, 2, 3]
}
const [reducer, actions] = reduxLeaves(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]