Skip to content

useArrayFindLast

Category
Export Size
124 B
Last Changed
5 days ago

Reactive Array.findLast.

Demo

Usage

tsx
import { 
useArrayFindLast
} from '@reause/shared'
const
list
= [1, -1, 2]
const
positive
=
useArrayFindLast
(
list
,
val
=>
val
> 0) // 2

Use with reactive array

tsx
import { 
useArrayFindLast
} from '@reause/shared'
import {
useState
} from 'react'
const [
list
,
setList
] =
useState
([-1, -2])
const
positive
=
useArrayFindLast
(
list
,
val
=>
val
> 0) // undefined
setList
([...
list
, 10])
// positive: 10 on the next render
setList
([...
list
, 5])
// positive: 5 on the next render

list is a plain read-only array: pass the array directly (e.g. from useState), or ref.current if you keep it in a ref. The result recomputes on the render that passes a new array.

Type Declarations

ts
export type UseArrayFindLastReturn<T = any> = T | undefined

export function useArrayFindLast<T>(list: readonly T[], fn: (element: T, index: number, array: readonly T[]) => boolean): UseArrayFindLastReturn<T>

Source

Source · Demo · VueUse

Contributors

hairyf

Changelog

v0.1.0 on
bfd6a - fix(shared): resolve useArrayFindLast audit findings (#718)
8fddc - chore!: remove all Vue-only Maybe* types and getter unions, adopt React Ref (#462)

Released under the MIT License. v0.1.8