gatsby-ecommerce-site icon indicating copy to clipboard operation
gatsby-ecommerce-site copied to clipboard

Updating state while a component is rendering

Open andria-dev opened this issue 4 years ago • 0 comments

https://github.com/dayhaysoos/gatsby-ecommerce-site/blob/fea28a917e231914ec3c19e06d59c56b02375a68/src/pages/success.js#L6-L9

On your success page, it looks like you're performing a state update during the render of a component. In React, it will warn you when you do this with something like:

Cannot update a component (CartProvider) while rendering a different component (App).

It would be better if you did use a useEffect. The following would only run the useEffect once and would not run it during rendering.

useEffect(() => {
  clearCart();
}, [clearCart]);

andria-dev avatar Jan 18 '21 16:01 andria-dev