mirror of
https://github.com/HSLdevcom/digitransit-ui
synced 2025-07-28 08:05:18 +02:00
27 lines
495 B
JavaScript
27 lines
495 B
JavaScript
import { useRef } from 'react';
|
|
|
|
const usePrevious = (value, comparingFunc) => {
|
|
const ref = useRef({
|
|
value,
|
|
prev: null,
|
|
});
|
|
|
|
const current = ref.current.value;
|
|
let isEqual = false;
|
|
|
|
if (
|
|
comparingFunc
|
|
? !comparingFunc(current, value)
|
|
: JSON.stringify(value) !== JSON.stringify(current)
|
|
) {
|
|
ref.current = {
|
|
value,
|
|
prev: current,
|
|
};
|
|
isEqual = true;
|
|
}
|
|
|
|
return { previous: ref.current.prev, isEqual };
|
|
};
|
|
|
|
export default usePrevious;
|