Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/use array hook #597

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function Component() {

<!-- HOOKS:START -->

- [`useArray`](https://usehooks-ts.com/react-hook/use-array) — handles Array state with useful utility functions.
- [`useBoolean`](https://usehooks-ts.com/react-hook/use-boolean) — handles boolean state with useful utility functions.
- [`useClickAnyWhere`](https://usehooks-ts.com/react-hook/use-click-any-where) — handles click events anywhere on the document.
- [`useCopyToClipboard`](https://usehooks-ts.com/react-hook/use-copy-to-clipboard) — copies text to the clipboard using the [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API).
Expand Down
1 change: 1 addition & 0 deletions packages/usehooks-ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function Component() {

<!-- HOOKS:START -->

- [`useArray`](https://usehooks-ts.com/react-hook/use-array) — handles Array state with useful utility functions.
- [`useBoolean`](https://usehooks-ts.com/react-hook/use-boolean) — handles boolean state with useful utility functions.
- [`useClickAnyWhere`](https://usehooks-ts.com/react-hook/use-click-any-where) — handles click events anywhere on the document.
- [`useCopyToClipboard`](https://usehooks-ts.com/react-hook/use-copy-to-clipboard) — copies text to the clipboard using the [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API).
Expand Down
1 change: 1 addition & 0 deletions packages/usehooks-ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './useArray'
export * from './useBoolean'
export * from './useClickAnyWhere'
export * from './useCopyToClipboard'
Expand Down
1 change: 1 addition & 0 deletions packages/usehooks-ts/src/useArray/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useArray'
27 changes: 27 additions & 0 deletions packages/usehooks-ts/src/useArray/useArray.demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useArray } from './useArray'

export default function Component() {
const { value, push, removeByIndex } = useArray<number>([1, 2, 3])

return (
<>
<div>
<div>Array: {JSON.stringify(value)}</div>
<button
onClick={() => {
push(4)
}}
>
Add 4
</button>
<button
onClick={() => {
removeByIndex(1)
}}
>
Remove at index 1
</button>
</div>
</>
)
}
1 change: 1 addition & 0 deletions packages/usehooks-ts/src/useArray/useArray.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The useArray hook is a custom React hook designed to manage arrays in a React component state. It provides functions to manipulate the array, such as adding elements and removing elements by index.
26 changes: 26 additions & 0 deletions packages/usehooks-ts/src/useArray/useArray.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { act, renderHook } from '@testing-library/react'

import { useArray } from './useArray'

describe('useArray hook', () => {
it('should initialize with the provided initial value', () => {
const { result } = renderHook(() => useArray([1, 2, 3]))
expect(result.current.value).toEqual([1, 2, 3])
})

it('should add an element to the array', () => {
const { result } = renderHook(() => useArray([1, 2, 3]))
act(() => {
result.current.push(4)
})
expect(result.current.value).toEqual([1, 2, 3, 4])
})

it('should remove an element from the array by index', () => {
const { result } = renderHook(() => useArray([1, 2, 3]))
act(() => {
result.current.removeByIndex(1)
})
expect(result.current.value).toEqual([1, 3])
})
})
38 changes: 38 additions & 0 deletions packages/usehooks-ts/src/useArray/useArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useState } from 'react'

/** This defines the return type of the custom hook `useArray`. */
type UseArrayReturnType<T> = {
/** An array containing elements of type T. */
value: T[]
/** A function that adds an element of type T to the array. */
push: (element: T) => void
/** A function that removes an element from the array by its index. */
removeByIndex: (index: number) => void
}

/**
* Custom hook that handles Array state with useful utility functions.
* @param {[]} [initialValue] - The initial value for the Array state (default is `[]`).
* @returns {UseArrayReturnType} An object containing the utility functions to manipulate the state.
* @throws Will throw an error if `initialValue` is an invalid array value.
* @public
* @see Yet..
* @example
* ```tsx
* const { value, push, removeByIndex } = useArray<number>([1, 2, 3]);
* ```
*/

export function useArray<T>(initialValue: T[]): UseArrayReturnType<T> {
const [array, setArray] = useState<T[]>(initialValue)

const push = (element: T) => {
setArray(prevArray => [...prevArray, element])
}

const removeByIndex = (index: number) => {
setArray(prevArray => prevArray.filter((_, i) => i !== index))
}

return { value: array, push, removeByIndex }
}