0

Although there is a change in State and useEffect is used to refresh the page when there is a change ... The state changes normally but there is some delay in redefining the state so the page is not interpreted correctly. Attach the code I would love for answers... Thanks in advance.

let { category } = useParams();
const [products, setProducts] = useState([]);
const [filterProducts, setFilterProducts] = useState([]);
useEffect(async () => {
    try {
        let res = await utils.GetAllItems("http://localhost:5000/product");
        setProducts(res.data.Products);
    }
    catch (error) {
        alert(error.response.data.message);
    }
},[])

*useEffect(()=>
{
    if(category != "all") {
        let p = products.filter(x => x.Category == category);
        console.log(p);
        setFilterProducts(p);
        console.log(filterProducts);
    }
    else {
        setFilterProducts(products);
    }*
},[category])
skyboyer
  • 19,620
  • 7
  • 50
  • 62
Yehuda
  • 1
  • Reproducing the problem in codesandbox will help people to understand what do you mean in generic sayings like "not work properly", "changes normally", "not interpreted correctly" – Dennis Vash Feb 17 '22 at 10:18

0 Answers0