I am trying to update the state with an array. But I need to keep the existing information in the array and just push more information into it.
Here is my state:
  const [filteredProducts, setFilteredProducts] = useState([]);
This is my function I am calling for the onClick event listener
const filteredByCategory = (event, category, products) => {
  const element = document.getElementById(event.target.id);
  
  if (element.checked) {
    const productsToAdd = products.filter(
      (product) => product.category === category
    );
    
    setFilteredProducts((currentFilteredProducts) => [
      ...currentFilteredProducts,
      ...productsToAdd,
    ]);
    
    dispatch({
      type: "PRODUCTS_FILTERED",
      payload: filteredProducts,
    });
  } else {
    let removedCheckedProducts = filteredProducts.filter((product) => {
      return product.category !== category;
    });
    
    setFilteredProducts(removedCheckedProducts);
    
    if (removedCheckedProducts.length >= 1) {
      dispatch({
        type: "PRODUCTS_FILTERED",
        payload: removedCheckedProducts,
      });
    } else {
      dispatch({
        type: "PRODUCTS_FILTERED",
        payload: allProducts,
      });
    }
  }
};
 
     
     
    