Hey guys I've been learning react for a few weeks now so please be easy on me =). When I was using dummy data, the filter function worked and showed the correct products in the category. I built the back end api using django and now my filter function doesn't work anymore. It does filter but the data totally disappears after pressing the different filter buttons. Can anyone help?
import React, { useState, useEffect } from "react";
import axios from "axios";
import ButtonList from "../components/ButtonList";
import ProductList from "../components/ProductList";
const ProductPage = () => {
const [products, setProducts] = useState([]);
useEffect(() => {
const fetchProduct = async () => {
const { data } = await axios.get("/api/products/");
setProducts(data);
};
fetchProduct();
}, []);
const filter = (button) => {
if (button === "All") {
setProducts(products);
return;
}
const filteredData = products.filter(
(products) => products.category === button
);
setProducts(filteredData);
};
return (
<div>
<ButtonList onClickFilter={filter} />
<ProductList product={products} />
</div>
);
};
export default ProductPage;