The useArray Hook is used to easily deal with arrays, without having to use the rest/spread syntax.
The Array Hook returns an object with the following:
- value: The current array
- setValue: Sets a new array as the value
- add: Adds a given element to the array
- clear: Removes all elements from the array
- removeIndex: Removes an element from the array by its index
- removeById: Removes an element from the array by its id (assuming that the elements in the array are objects with an id key)
It works as follows:
- First, we import the useArray Hook from react-hookedup:
import React from 'react'import { useArray } from 'react-hookedup'
- Then, we define the component and the Array Hook, with the default value of ['one', 'two', 'three'] ...