uniswap-interface-uncensored/src/hooks/useLast.ts
Zach Pomerantz e81e8a8f71
fix: inadvertent merges/reverts (#2915)
* Revert "Revert "Merge branch 'main' of https://github.com/Uniswap/interface" (#2912)"

This reverts commit 7d343dcfbdf75a2f91d28cefce84e4b1bace7b87.

* Revert "deleted files"

This reverts commit 097b8361d4c09afd3cb681c4622145c555ced884.
2021-12-02 10:35:39 -08:00

22 lines
719 B
TypeScript

import { useEffect, useState } from 'react'
/**
* Returns the last value of type T that passes a filter function
* @param value changing value
* @param filterFn function that determines whether a given value should be considered for the last value
*/
export default function useLast<T>(
value: T | undefined | null,
filterFn?: (value: T | null | undefined) => boolean
): T | null | undefined {
const [last, setLast] = useState<T | null | undefined>(filterFn && filterFn(value) ? value : undefined)
useEffect(() => {
setLast((last) => {
const shouldUse: boolean = filterFn ? filterFn(value) : true
if (shouldUse) return value
return last
})
}, [filterFn, value])
return last
}