// using JSON.stringify(object)
useEffect(() => {
// your code here...
}, [JSON.stringify(dependencyObject)]);
The best solution is to use JSON.stringify(object)
as it won't lead to any error on the initial load or warning about the changes in size of dependency variables.
// using spread operator
useEffect(() => {
// your code here...
}, [ ...Object.values(dependencyObject) ]);
The solution with spread operator on object keys and values will cause an error if the object is null/undefined in the initial load.
/*
Also if you make a custom function that either returns the
values or empty array then React will give a warning about
the size change in dependency array.
*/
const getDependencies = (addressType: Address) => {
if (addressType) {
return Object.values(addressType);
}
return [];
}
useEffect(() => {
// your code here...
}, [ ...getDependencies(dependencyObject) ]);
So use JSON.stringyfy(object). it won't give any errors when the object is undefined or null and React won't complain about the change in size of dependency variables.