import { useState, useEffect } from "react"; import axios from "axios"; import styled…
- User
import { useState, useEffect } from "react"; import axios from "axios"; import styled from "styled-components"; import { popularProducts } from "../data"; import Product from "./Product"; import { mobile } from "../responsive"; type ProductProps = { _id: string; img: string; [key: string]: string };
const Container = styled.div
padding: 20px; display: flex; flex-wrap: wrap; justify-content: space-between; ${mobile({ padding: "0" })};; type Filters = { [key: string]: string; }; const Products: React.FC<{ cat?: string; filters?: Filters; sort?: string; }> = ({ cat, filters, sort }) => { const [products, setProducts] = useState<ProductProps[]>([]); const [filteredProducts, setFilteredProducts] = useState<ProductProps[]>([]); console.log(filters); useEffect(() => { const getProducts = async () => { try { const res = await axios.get( cat ?http://localhost:5000/api/products?category=${cat}: "http://localhost:5000/api/products" );setProducts(res.data); } catch (err) { console.log(err); } }; getProducts();}, [cat]); useEffect(() => { const getFilters = async () => { if (filters) setFilteredProducts( filters.color === "none" ? products : products.filter((item) => item.color[0] === filters.color) ); }; getFilters(); }, [products, filters?.color, cat]); console.log(filteredProducts); return ( {filteredProducts.map((product, index) => ( ))} ); };
export default Products; how to start second useeffect?
- Assistant
You can start the second
useEffectby updating the dependencies array in theuseEffecthook and add the variables that it depends on. For example, if you want to trigger the seconduseEffectwhen thecat,products, andfiltersstate variables change, you can update the dependencies array like this:useEffect(() => { // This effect depends on cat, products, and filters }, [cat, products, filters]);Then you can add the logic for the second effect inside the function body. Note that you can have multiple
useEffecthooks in a single component, so you can add as many effects as you need. - User
const ProductsList = () => { const location = useLocation(); const cat = location.pathname.split("/")[2]; const [filters, setFilter] = useState({}); const [sort, setSort] = useState("newest"); const handleFilters = (e: React.ChangeEvent) => { const value = e.target.value; setFilter({ ...filters, [e.target.name]: value }); }; return ( Filter Products: colors none blue green black Sort Products: <Select onChange={(e) => setSort(e.target.value)}> newest asc desc ); };
export default ProductsList; how send on select by default value?
- Assistant
To set a default value for a
<select>element, you can use thevalueattribute on the<select>element itself or on the individual<option>elements. Here's an example of setting a default value for the color filter:const ProductsList = () => { const location = useLocation(); const cat = location.pathname.split("/")[2]; const [filters, setFilter] = useState({ color: "none" // Set the default value for the color filter to "none" }); const [sort, setSort] = useState("newest"); const handleFilters = (e: React.ChangeEvent<HTMLSelectElement>) => { const value = e.target.value; setFilter({ ...filters, [e.target.name]: value }); }; return ( <Container> <FilterContainer> <Filter> <FilterText>Filter Products:</FilterText> <Select name="color" value={filters.color} onChange={handleFilters}> <Option disabled>colors</Option> <Option value="none">none</Option> <Option value="blue">blue</Option> <Option value="green">green</Option> <Option value="black">black</Option> </Select> </Filter> <Filter> <FilterText>Sort Products:</FilterText> <Select onChange={(e) => setSort(e.target.value)} value={sort}> <Option value="newest">newest</Option> <Option value="asc">asc</Option> <Option value="desc">desc</Option> </Select> </Filter> </FilterContainer> <Products cat={cat} filters={filters} sort={sort} /> </Container> ); }; export default ProductsList; - User
useEffect(() => { if (sort === "newest") { setFilteredProducts((prev) => [...prev].sort((a, b) => a.createdAt - b.createdAt) ); } else if (sort === "oldest") { setFilteredProducts((prev) => [...prev].sort((a, b) => b.createdAt - a.createdAt) ); } }, [sort]); is itcorrect?
- Assistant