gatsby-shopify-starter icon indicating copy to clipboard operation
gatsby-shopify-starter copied to clipboard

ProductForm availability check, dependency array bug?

Open abohannon opened this issue 3 years ago • 0 comments

Am I mistaken in thinking the dependency arrays are used incorrectly here. Won't client.product, variants, and productVariant always be different due to a lack of referential equality?

const checkAvailability = useCallback(
    productId => {
      client.product.fetch(productId).then(fetchedProduct => {
        // this checks the currently selected variant for availability
        const result = fetchedProduct.variants.filter(
          variant => variant.id === productVariant.shopifyId
        )
        if (result.length > 0) {
          setAvailable(result[0].available)
        }
      })
    },
    [client.product, productVariant.shopifyId, variants]
  )

  useEffect(() => {
    checkAvailability(product.shopifyId)
  }, [productVariant, checkAvailability, product.shopifyId])

abohannon avatar Nov 07 '20 21:11 abohannon