Created
May 25, 2023 14:30
-
-
Save danielturus/92486d21a8d54333f05c96a024278f6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react' | |
export function usePropertyChangeTracker({ data, property, callback }) { | |
const previousDataRef = React.useRef([]) | |
React.useEffect(() => { | |
const previousData = previousDataRef.current | |
for (let i = 0; i < data.length; i++) { | |
const previousItem = previousData[i] | |
const currentItem = data[i] | |
if (previousItem && previousItem[property] !== currentItem[property]) { | |
callback({ previous: previousItem, current: currentItem }) | |
} | |
} | |
previousDataRef.current = data | |
}, [data, property]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment