I have a useEffect() that should trigger when an array of objects changes.
useEffect(() => {
setNotesToRender(props.notes)
}, [props.notes]);
The useEffect dependency cannot compare arrays of objects, so the useEffect() in the example above triggers more often than needed. I only wish to trigger the useEffect() when the arrays differ.
An example of arrays that should trigger update
array1 = [{id:1,title:'one'},{id:2,title:'two'},{id:3,title:'three'}]
array2 = [{id:1,title:'one'},{id:2,title:'two'},{id:3,title:'four'}]
Things I have tested so far are:
- props.notes.length --> works but is not reliable - for obvious reasons :)
- ...props.notes --> does not work if array is empty - and may cause serious performance issues?
What is the most efficient way to compare two arrays of objects? The arrays may contain over 1000 objects.
Kind regards /K
UPDATE
I need to filter out certain "notes" later using a text search, that's why I am using a React state. This is not shown in my example code.