Easy
Why isn't the cart count updating immediately in this component?
1const ShoppingCart = () => {2 const [cartItems, setCartItems] = useState([]);3 const [cartCount, setCartCount] = useState(0);45 const addToCart = (product) => {6 setCartItems([...cartItems, product]);7 setCartCount(cartItems.length);8 };910 return (11 <div>12 <h2>Cart ({cartCount} items)</h2>13 <div className="grid grid-cols-2 gap-4">14 {cartItems.map(item => (15 <div key={item.id} className="p-2 border">16 <p>{item.name}</p>17 <p>${item.price}</p>18 </div>19 ))}20 </div>21 </div>22 );23};