Implement a useArray hook that manages an array of items with additional utility methods.
It is more convenient to use useArray than plain useState; with useState, you have to derive a new array and set state with that array for every update, which can be cumbersome.
The hook should work generically with arrays of any type.
const defaultValue = ['apple', 'banana'];export default function Component() {const { array, push, update, remove, filter, set, clear } =useArray(defaultValue);return (<div><p>Fruits: {array.join(', ')}</p><button onClick={() => push('orange')}>Add orange</button><button onClick={() => update(1, 'grape')}>Change second item to grape</button><button onClick={() => remove(0)}>Remove first</button><button onClick={() => filter((fruit) => fruit.includes('a'))}>Keep fruits containing 'a'</button><button onClick={() => set(defaultValue)}>Reset</button><button onClick={clear}>Clear list</button></div>);}
defaultValue: The initial array of itemsThe hook returns an object with the following properties:
array: The current array of itemsset: (newArray) => void: A function that sets the array of items. It must have the same type as the setter returned by useStatepush: (item) => void: A function that adds an item to the end of the arrayremove: (index: number) => void: A function that removes an item from the array by indexfilter: (predicate) => void: A function that filters the array based on a predicate function. predicate must have the same type as the callback passed to Array.prototype.filterupdate: (index: number, newItem) => void: A function that replaces an item in the array at indexclear: () => void: A function that clears the arrayarray directly.set should behave like the setter returned by useState, including accepting updater functions.clear() should replace the array with an empty array.console.log() statements will appear here.