const Container = styled.div padding: 20px; display: flex; flex-wrap: wrap;…
- User
const Container = styled.div
padding: 20px; display: flex; flex-wrap: wrap; justify-content: space-between; ${mobile({ padding: "0" })};; type Filters = { [key: string]: string | number; }; const Products: React.FC<{ cat?: string; filters?: Filters; sort?: string; }> = ({ cat, filters, sort }) => { const [products, setProducts] = useState([]); const [filteredProducts, setFilteredProducts] = useState([]); useEffect(() => { const getProducts = async () => { try { const res = await axios.get( cat ?http://localhost:5000/api/products?category=${cat}: "http://localhost:5000/api/products" ); console.log(res); } catch (err) { console.log(err); } }; getProducts(); }, [cat]); return ( {popularProducts.map((product) => ( ))} ); };export default Products; why console.log render 2 times?
- Assistant
The console.log is being called twice because it is being called inside the useEffect hook, which runs both when the component mounts and when the "cat" prop changes (due to the dependencies array of the useEffect hook). This means that the console.log will be called once on mount, and then again every time the "cat" prop changes.