So in my code below, I want to understand what is causing the component to re-render multiple times. Even though most of the time this code runs without any problem, but sometimes it causes the browser to become unresponsive.
I can't pinpoint what is causing that problem and believe that relooking at the useEffect might be the key to solving it.
Because the dispatch, props & addToast are dependent on success, should I remove them from the dependency array and just keep success and error?
Is there a way to figure out which dependency to keep and which to remove?
useEffect(() => {
if (success) {
dispatch({ type: PRODUCT_RESET });
dispatch(listProducts());
props.onHide();
addToast("Product has been added!", {
appearance: "success",
autoDismiss: false,
});
} else if (error) {
addToast(error, { appearance: "error", autoDismiss: false });
}
}, [dispatch, success, error, addToast, props]);