Skip to main content

Posts

Showing posts with the label useCallback

missing dependency for useEffect/useCallback when call function from useContext

#issue 1 was missing dependency: "e mptyShoppingCart" const { emptyShoppingCart } = useContext ( CartContext ) useEffect (() => { if ( oobCode ) { setOrderId ( oobCode ) emptyShoppingCart () } }, [ oobCode ]) so change to fix: useEffect (() => { if ( oobCode ) { setOrderId ( oobCode ) emptyShoppingCart () } }, [ oobCode, emptyShoppingCart ]) now #issue 2 is rendered forever because emptyShoppingCart always re-create fix: useCallback const emptyShoppingCart = useCallback (() => { setShoppingCart ([]) setListingId ( '' ) }, [])