Normally you wouldn't want to do that, but just to answer your question, it can be done, so let me propose the following assuming your list is called items
:
useEffect(() => {
}, [...items.map(v => v.country)])
What the above code does is to spread all items
(with its country
property) into the useEffect
dependency array.
The reason why this can be adhoc is mainly because React doesn't like to have a variable length of dependency. In the source code, when the length changes, it only appreciates the element change from the existing elements. So you might run into problem if you switch from 1 elements to 2 elements.
However if you have fixed number of elements, this should do what you wanted. Keep in mind the items
has to be an array at all time.
NOTE: to accommodate the length issue, maybe we can add an additional variable length
to the dependency array :)
}, [items.length, ...items.map(v => v.country)])
As i mentioned, most of time, you should avoid doing this, instead try to change the entire items
every time when an item changes. And let the Item
display to optimize, such as React.memo
.