Current Array:
React ×TypeScript ×Next.js ×
Array Operations:
Click on items to remove them. Use the operations above to manipulate the array.
Installation
Parameters
Parameter | Type | Default | Description |
---|
options | UseArrayStateOptions | {} | Configuration options for the hook |
UseArrayStateOptions
Property | Type | Default | Description |
---|
initialValue | T[] | [] | The initial array value |
onChange | (array: T[]) => void | null | Callback called whenever the array changes |
Return Value
The hook returns an object with the following properties and methods:
Property | Type | Description |
---|
array | T[] | The current array state |
length | number | The length of the array |
isEmpty | boolean | Whether the array is empty |
first | T | undefined | The first item in the array |
last | T | undefined | The last item in the array |
Mutating Methods
Method | Type | Description |
---|
push | (...items: T[]) => void | Adds items to the end of the array |
pop | () => T | undefined | Removes and returns the last item |
shift | () => T | undefined | Removes and returns the first item |
unshift | (...items: T[]) => void | Adds items to the beginning of the array |
insert | (index: number, ...items: T[]) => void | Inserts items at a specific index |
remove | (index: number) => void | Removes item at a specific index |
removeWhere | (predicate: (item: T, index: number) => boolean) => void | Removes items matching a condition |
update | (index: number, item: T) => void | Updates item at a specific index |
updateWhere | (predicate: (item: T, index: number) => boolean, newItem: T) => void | Updates first item matching a condition |
clear | () => void | Removes all items from the array |
reset | () => void | Resets array to initial value |
setValue | (newArray: T[]) => void | Sets the entire array to a new value |
filter | (predicate: (item: T, index: number) => boolean) => void | Filters the array in place |
sort | (compareFn?: (a: T, b: T) => number) => void | Sorts the array in place |
reverse | () => void | Reverses the array in place |
Read-only Methods
Method | Type | Description |
---|
map | <U>(transform: (item: T, index: number) => U) => U[] | Maps over the array without mutating |
find | (predicate: (item: T, index: number) => boolean) => T | undefined | Finds the first matching item |
findIndex | (predicate: (item: T, index: number) => boolean) => number | Finds the index of the first matching item |
includes | (item: T) => boolean | Checks if the array includes an item |
indexOf | (item: T) => number | Gets the index of an item |
slice | (start?: number, end?: number) => T[] | Returns a shallow copy of a portion |
Key Features & Details
Type Safety
- The hook is fully type-safe with TypeScript generics.
- All methods maintain type information throughout operations.
- Prevents runtime errors with proper type checking.
- Uses
useCallback
to memoize all methods for optimal performance.
- Prevents unnecessary re-renders by checking for actual changes.
- Efficiently handles large arrays with immutable updates.
Change Detection
- The
onChange
callback is called whenever the array state changes.
- Changes are detected at the reference level for performance.
- Callbacks receive the new array as their argument.
SSR Compatibility
- Fully compatible with Server-Side Rendering.
- No browser-specific APIs used.
- Safe for use in Next.js and other SSR frameworks.
Examples
Basic Usage
import { useArrayState } from '@/hooks/guarahooks/use-array-state';
function TodoList() {
const {
array: todos,
push,
remove,
clear,
length,
isEmpty,
} = useArrayState<string>({
initialValue: ['Learn React', 'Build an app'],
});
const addTodo = (todo: string) => {
push(todo);
};
return (
<div>
<p>You have {length} todos</p>
{isEmpty ? (
<p>No todos yet!</p>
) : (
<ul>
{todos.map((todo, index) => (
<li key={index}>
{todo}
<button onClick={() => remove(index)}>Remove</button>
</li>
))}
</ul>
)}
<button onClick={clear}>Clear All</button>
</div>
);
}
With onChange Callback
const { array, push, remove } = useArrayState<number>({
initialValue: [1, 2, 3],
onChange: (newArray) => {
console.log('Array changed:', newArray);
// Sync with localStorage, API, etc.
},
});
Advanced Array Operations
const { array, push, filter, sort, updateWhere, removeWhere, first, last } =
useArrayState<{ id: number; name: string; active: boolean }>({
initialValue: [
{ id: 1, name: 'John', active: true },
{ id: 2, name: 'Jane', active: false },
],
});
// Add new user
const addUser = (name: string) => {
push({ id: Date.now(), name, active: true });
};
// Toggle user status
const toggleUser = (id: number) => {
updateWhere((user) => user.id === id, {
...find((user) => user.id === id)!,
active: !user.active,
});
};
// Remove inactive users
const removeInactiveUsers = () => {
removeWhere((user) => !user.active);
};
// Sort by name
const sortByName = () => {
sort((a, b) => a.name.localeCompare(b.name));
};
Search and Filter
const {
array: items,
filter,
setValue,
reset,
} = useArrayState<string>({
initialValue: ['apple', 'banana', 'cherry', 'date'],
});
const searchItems = (query: string) => {
if (!query) {
reset(); // Return to original list
} else {
filter((item) => item.toLowerCase().includes(query.toLowerCase()));
}
};
Dynamic List Management
const {
array: queue,
push,
shift,
length,
first,
isEmpty,
} = useArrayState<{ id: string; message: string; timestamp: number }>();
// Add to queue
const enqueue = (message: string) => {
push({
id: crypto.randomUUID(),
message,
timestamp: Date.now(),
});
};
// Process next item
const dequeue = () => {
return shift();
};
// Queue status
const getQueueStatus = () => ({
size: length,
next: first,
isEmpty,
});