useArray to work with arrays in useState and updating to the DOM.
To init it defaults to an empty array, or takes an array as an input, and returns a selection of helper functions.
npm i @sjblurton/use-array
yarn add @sjblurton/use-array
import useArray from "@sjblurton/use-array";
const { array, set, clear, filter, push, remove, update, reverse, sort } = useArray([1, 2, 3, 4, 5, 6, 7,])
sets the array to a new value.
set([4,3,2,1])
clears the array to an empty array
clear()
array filter function. add the callback function to filter.
filter(n => n < 4)
add a new element to the end of the array.
push('hello world')
remove the element at the given index.
remove(2)
update the element in the array of the given index.
update(2, 'fo')
reverses the array
reverse()
sorts the array. Takes a function or undefined
sort()
sort((a, b) => {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
})
GitHub: https://github.com/sjblurton/use-array
NPM: https://www.npmjs.com/package/@sjblurton/use-array