Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions react/usearray_en.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,44 @@
Requirements
- `removeAtIndex` method
- `push` method

There is no solution yet.
Now, the implementation of the above methods are standard problems with the notable exception that we must update the array immutably each time. This necessity arises due to the requirement of re-rendering the component wherever this hook is utilized. React employs a strict equality check for optimization purposes, as explained in this [article](https://react.dev/learn/updating-objects-in-state), to differentiate state changes.

Would you like to [contribute to the solution](https://github.com/BFEdev/BFE.dev-solutions/blob/main/react/usearray_en.md)? [Contribute guideline](https://github.com/BFEdev/BFE.dev-solutions#how-to-contribute)
```
// Solution by [Ankit Kumar](https://github.com/mynameisankit)

import { useState, useCallback, Dispatch, SetStateAction } from 'react'

type UseArrayActions<T> = {
push: (item: T) => void,
removeByIndex: (index: number) => void
}

function removeAtIndex<T>(index: number, array: T[], setArray: Dispatch<SetStateAction<T[]>>): void {
const partBeforeIndex = array.slice(0, index) || [];
const partAfterIndex = array.slice(index + 1) || [];

const updatedArray = [...partBeforeIndex, ...partAfterIndex];

setArray(updatedArray);
}

function pushValue<T>(value: T, array: T[], setArray: Dispatch<SetStateAction<T[]>>): void {
const updatedArray = [...array, value];

setArray(updatedArray);
}

export function useArray<T>(initialValue: T[]= []): { value: T[] } & UseArrayActions<T> {
const [array, setArray] = useState([...initialValue]);

const removeByIndex = useCallback((index: number) => removeAtIndex(index, array, setArray), []);
const push = useCallback((value: T) => pushValue(value, array, setArray), []);

return {
value: array,
removeByIndex,
push,
};
}
```