What’s the recommended approach for determining which states to include in useEffect’s dependency array?
I thought I should only pass states that I wanna watch over for changes and fire the useEffect based on those changes.
For example in the following useEffect I only passed actionData on the dependency list as I this useEffect needs to be fired when I get some specific actionData. But I’m getting the following warning: React Hook useEffect has a missing dependency: 'formState'. Either include it or remove the dependency array.
useEffect(() => {
const currentState = JSON.parse(JSON.stringify(formState));
if (actionData && actionData?.target === "productVariants" && currentState.selectedProducts.length > 0) {
// Finding the product from the formStates selectedProducts array
const selectedProduct = currentState.selectedProducts.find(product => product.selectionId === actionData.selectionId );
const selectedVariantIdsAndQty = selectedProduct?.variants.map(variant => ({
variantId: variant.variantId,
variantQty: variant.quantity,
title: variant.title,
price: variant.price,
selectedOptionsValue: variant.selectedOptionsValue,
})); // Extract already selected product variants Id and qty
// Setting the selected variants with id and qty in the selectedVariants array
setSelectedProductVariantsAndQty(selectedVariantIdsAndQty);
// Setting all variants of this product in the allProductVariants array
setAllProductVariants(actionData);
// Setting modal loading state to false.
setModalLoading(false);
}
}, [actionData]);
Thanks!