Commit dc829dd5 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

fix: products preview can empty when switch to another page and come

back
parent c1d781cb
...@@ -42,7 +42,9 @@ export default function Home() { ...@@ -42,7 +42,9 @@ export default function Home() {
} }
setAlgorithm(productRandom); setAlgorithm(productRandom);
} }
}, [products]);
useEffect(() => {
// products filter for search and category // products filter for search and category
let product_cache = let product_cache =
!!router.query?.q && router.query?.q?.length !!router.query?.q && router.query?.q?.length
...@@ -60,7 +62,7 @@ export default function Home() { ...@@ -60,7 +62,7 @@ export default function Home() {
Number(products[pid].stock) > 0 Number(products[pid].stock) > 0
); );
setProductsFilter(product_cache); setProductsFilter(product_cache);
}, [products, category]); }, [products, category, algorithm]);
// for user click cart button // for user click cart button
async function onCart(id, isRemove = false) { async function onCart(id, isRemove = false) {
...@@ -121,7 +123,6 @@ export default function Home() { ...@@ -121,7 +123,6 @@ export default function Home() {
let response = await axios.get("/api/product"); let response = await axios.get("/api/product");
let pd = {}; let pd = {};
response.data.map((e) => (pd[e.id] = e)); response.data.map((e) => (pd[e.id] = e));
setProducts(pd); setProducts(pd);
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment