Skip to content

Commit

Permalink
feat(usePreviousDistinct): improve types;
Browse files Browse the repository at this point in the history
  • Loading branch information
xobotyi committed Nov 4, 2019
1 parent d4ff80a commit 30f53e8
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/usePreviousDistinct.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { useRef } from 'react';

function strictEquals<T>(prev: T | undefined, next: T) {
return prev === next;
}
export type Predicate<T> = (prev: T | undefined, next: T) => boolean;

const strictEquals = <T>(prev: T | undefined, next: T) => prev === next;

export default function usePreviousDistinct<T>(
value: T,
compare: (prev: T | undefined, next: T) => boolean = strictEquals
) {
export default function usePreviousDistinct<T>(value: T, compare: Predicate<T> = strictEquals): T | undefined {
const prevRef = useRef<T>();
const curRef = useRef<T>();

if (!compare(curRef.current, value)) {
prevRef.current = curRef.current;
curRef.current = value;
Expand Down

0 comments on commit 30f53e8

Please sign in to comment.