const ProductPage = () => { const [products, setProducts] = useState<Product[]>(); const…
- User
const ProductPage = () => { const [products, setProducts] = useState<Product[]>(); const [filter, setFilter] = useState<Product[]>(); const [quantity, setQuantity] = useState(1); const location = useLocation(); const id = location.pathname.split("/")[2]; const dispatch = useAppDispatch(); const handleQuantity = (type: string) => { if (quantity > 1 && type === "dec") setQuantity(quantity - 1); if (type === "inc") setQuantity(quantity + 1); }; const handleClick = () => { dispatch(addProduct({ products, quantity })); }; useEffect(() => { const getProducts = async () => { try { const res = await publicRequest.get(
/products/find/${id}); setProducts(res.data); } catch (err) { console.log(err); } }; getProducts(); }, [id]); useEffect(() => { const getFilter = async () => { try { const res = await axios.get(http://localhost:5000/api/products); setFilter(res.data); } catch (err) { console.log(err); } }; getFilter(); }, [products?.color]); return ( {products && filter ? ( <> {products.title} {products?.description} {products.price}PLN Color {filter .filter((item) => item.color[0] !== products.color[0]) .map((item, index) => ( <Link key={index} to={/product/${item._id}}> ))} <RemoveIcon onClick={() => handleQuantity("dec")} /> {quantity} <AddIcon onClick={() => handleQuantity("inc")} /> <Button onClick={() => handleClick()}>Add to cart </> ) : null} ); }; const ProductPage = () => { const [products, setProducts] = useState<Product[]>(); const [filter, setFilter] = useState<Product[]>(); const [quantity, setQuantity] = useState(1); const location = useLocation(); const id = location.pathname.split("/")[2]; const dispatch = useAppDispatch(); const handleQuantity = (type: string) => { if (quantity > 1 && type === "dec") setQuantity(quantity - 1); if (type === "inc") setQuantity(quantity + 1); }; const handleClick = () => { dispatch(addProduct({ products, quantity })); }; useEffect(() => { const getProducts = async () => { try { const res = await publicRequest.get(/products/find/${id}); setProducts(res.data); } catch (err) { console.log(err); } }; getProducts(); }, [id]); useEffect(() => { const getFilter = async () => { try { const res = await axios.get(http://localhost:5000/api/products); setFilter(res.data); } catch (err) { console.log(err); } }; getFilter(); }, [products?.color]); return ( {products && filter ? ( <> {products.title} {products?.description} {products.price}PLN Color {filter .filter((item) => item.color[0] !== products.color[0]) .map((item, index) => ( <Link key={index} to={/product/${item._id}}> ))} <RemoveIcon onClick={() => handleQuantity("dec")} /> {quantity} <AddIcon onClick={() => handleQuantity("inc")} /> <Button onClick={() => handleClick()}>Add to cart </> ) : null} ); }; const ProductPage = () => { const [products, setProducts] = useState<Product[]>(); const [filter, setFilter] = useState<Product[]>(); const [quantity, setQuantity] = useState(1); const location = useLocation(); const id = location.pathname.split("/")[2]; const dispatch = useAppDispatch(); const handleQuantity = (type: string) => { if (quantity > 1 && type === "dec") setQuantity(quantity - 1); if (type === "inc") setQuantity(quantity + 1); }; const handleClick = () => { dispatch(addProduct({ products, quantity })); }; useEffect(() => { const getProducts = async () => { try { const res = await publicRequest.get(); setProducts(res.data); } catch (err) { console.log(err); } }; getProducts(); }, [id]); useEffect(() => { const getFilter = async () => { try { const res = await axios.get(); setFilter(res.data); } catch (err) { console.log(err); } }; getFilter(); }, [products?.color]); return ( {products && filter ? ( <> {products.title} {products?.description} {products.price}PLN Color {filter .filter((item) => item.color[0] !== products.color[0]) .map((item, index) => ( <Link key={index} to={}> ))} <RemoveIcon onClick={() => handleQuantity("dec")} /> {quantity} <AddIcon onClick={() => handleQuantity("inc")} /> <Button onClick={() => handleClick()}>Add to cart </> ) : null} ); }; Type 'Product[] | undefined' is not assignable to type 'Product[]'. Type 'undefined' is not assignable to type 'Product[]'.ts(2322) - Assistant
This error is occurring because the
productsstate is initially undefined and is only set after the API call is made in theuseEffecthook. This means that when thefilterstate is being set in the seconduseEffecthook, theproducts?.colorexpression may be undefined, causing the filter API call to fail.To fix this issue, you can add a check for the
productsstate before making the filter API call in the seconduseEffecthook:typescriptuseEffect(() => { const getFilter = async () => { try { if (products?.color) { // add check for products.color before making API call const res = await axios.get(`http://localhost:5000/api/products`); setFilter(res.data); } } catch (err) { console.log(err); } }; getFilter(); }, [products?.color]);This will ensure that the filter API call is only made if the
productsstate exists and has a non-null value for the property.