@hookstate/mutate
v1.2.3
Published
Plugin for @hookstate/core to add mutate actions specific for arrays, objects, strings and numbers.
Downloads
6
Readme
@hookstate/mutate
Plugin for @hookstate/core to add mutate actions specific for arrays, objects, strings and numbers. See demo.
API Documentation
Array state
There the following array mutation actions available:
set([...])
orset((prevState) => [...])
sets new value of the array state. It has got the same behaviour as the second value returned from theReact.useState
functionmerge({...})
ormerge((prevState) => ({...}))
sets new value of the array state, updating the provided elements of the array, for example:merge({ 0: 'the first element is updated', 4: 'and the fifth too', })
Note:
prevState
variable in the callback is a clone/copy of the current array stateupdate(index, newElementValue)
orupdate(index, (prevElementValue) => newElementValue)
sets new value of the array state, updating the element of an array by the specified indexconcat([...])
orconcat((prevState) => [...])
sets new value of the array state, appending the provided array to the end of the current array.Note:
prevState
variable in the callback is a clone/copy of the current array statepush(newElement)
sets new value of the array state, adding new element to the endpop()
sets new value of the array state, removing the last elementinsert(indexWhereToInsert, newElement)
sets new value of the array state, inserting the new element by the specified indexremove(index)
sets new value of the array state, removing the element by the specified indexswap(index1, index2)
sets new value of the array state, swapping two elements by the specified indexes
Object state
There the following object mutation actions available:
set([...])
orset((prevState) => [...])
sets new value of the object state. It has got the same behaviour as the second value returned from theReact.useState
functionmerge({...})
ormerge((prevState) => ({...}))
sets new value of the object state, updating the specified propertiesupdate(propertyKey, newPropertyValue)
orupdate(propertyKey, (prevPropertyValue) => newPropertyValue)
sets new value of the object state, updating the specified property