Skip to content

Instantly share code, notes, and snippets.

@danielturus
Created May 25, 2023 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielturus/92486d21a8d54333f05c96a024278f6a to your computer and use it in GitHub Desktop.
Save danielturus/92486d21a8d54333f05c96a024278f6a to your computer and use it in GitHub Desktop.
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