Skip to content

sjblurton/use-array

Repository files navigation

useArray

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.

install

npm i @sjblurton/use-array

yarn add @sjblurton/use-array

Import useArray

import useArray from "@sjblurton/use-array";

To call the hook...

const { array, set, clear, filter, push, remove, update, reverse, sort } = useArray([1, 2, 3, 4, 5, 6, 7,])

set

sets the array to a new value.

set([4,3,2,1])

clear

clears the array to an empty array

clear()

filter

array filter function. add the callback function to filter.

filter(n => n < 4)

push

add a new element to the end of the array.

push('hello world')

remove

remove the element at the given index.

remove(2)

update

update the element in the array of the given index.

update(2, 'fo')

reverse

reverses the array

reverse()

sort

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;
        })

Links

GitHub: https://github.com/sjblurton/use-array
NPM: https://www.npmjs.com/package/@sjblurton/use-array

About

React custom hook useArray. Making arrays mush easier in React.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages