UPD: fix infinity loop in Order useEffect

This commit is contained in:
John Huang@tthqws02 2021-11-23 19:01:12 -05:00
parent decb754215
commit 52c2bde094
2 changed files with 6 additions and 7 deletions

View File

@ -14,14 +14,11 @@ const Details = () => {
// refresh the cart logo and amount of the cart // refresh the cart logo and amount of the cart
useEffect(() => { useEffect(() => {
let sub_total = cart.reduce((sum, it)=>{ let sub_total = cart.reduce((sum, it)=>{
console.log(it.price); console.log(it.price);
return it.price+sum return it.price+sum
},0); },0);
setTotal(sub_total); setTotal(sub_total);
console.log("effect", total) console.log("effect", total)
},[cart, total]); },[cart, total]);
@ -29,7 +26,7 @@ const Details = () => {
<> <>
<ShowCase /> <ShowCase />
{/* you can try 600011 in orderID */} {/* you can try 600011 in orderID */}
<Orders orderID={orderID} setOrderID={setOrderID} invoice={invoice} /> <Orders orderID={orderID} setOrderID={setOrderID} invoice={invoice} setInvoice={setInvoice} />
<AllProducts setCart={setCart} /> <AllProducts setCart={setCart} />
<CartIcon count={cart?.length} total={total} /> <CartIcon count={cart?.length} total={total} />
<Purchase /> <Purchase />

View File

@ -37,7 +37,7 @@ const invoiceShape = (order) => {
} }
const Orders = ({ orderID, setOrderID, invoice }) => { const Orders = ({ orderID, setOrderID, invoice, setInvoice }) => {
const orders = useGetOrders(orderID); const orders = useGetOrders(orderID);
const [results, setOrder] = useSetOrder(); const [results, setOrder] = useSetOrder();
@ -50,11 +50,13 @@ const Orders = ({ orderID, setOrderID, invoice }) => {
}, [results, setOrderID]); //didUpdate }, [results, setOrderID]); //didUpdate
useEffect(() => { useEffect(() => {
console.log("Invoice: ", invoice && invoiceShape(invoice));
if (invoice) { if (invoice) {
console.log("Invoice: ", invoiceShape(invoice)); // console.log("Invoice: ", invoiceShape(invoice));
setOrder(invoiceShape(invoice)); setOrder(invoiceShape(invoice));
setInvoice(null);
} }
}, [invoice]); //missing dep warning }, [invoice, setOrder, setInvoice]); //missing dep warning
const onClick = (event) => { const onClick = (event) => {
setOrder(sampleOrder); setOrder(sampleOrder);