Communication between components with React
Design comparison
Solution retrospective
I'm proud of the moment I managed to make the quantity of the product in the cart add up to the quantity I placed every time I clicked the add to cart button. It was a detail in the project that I had a lot of difficulty developing.
What challenges did you encounter, and how did you overcome them?I had a lot of difficulty to manage the quantity of products sending to minicart. The way that I found to solve that was:
export function Minicart({ onAddProductToCart }: MinicartProps) {
const products = onAddProductToCart();
const [productInTheMinicart, setProductInTheMinicart] =
useState();
const [quantityOfProductInTheMinicart, setQuantityOfProductInTheMinicart] =
useState(0);
useEffect(() => {
if (!products) return;
setProductInTheMinicart(products);
}, [products]);
useEffect(() => {
if (productInTheMinicart?.quantity !== undefined) {
setQuantityOfProductInTheMinicart(
(state) => state + productInTheMinicart?.quantity
);
}
}, [productInTheMinicart?.quantity]);
...
What specific areas of your project would you like help with?
I want to know if there's another efficent way to solve the problem that I explained in the first question.
Community feedback
- @javila26Posted 6 months ago
Hi María,
Great project, I used it as reference when building my solution. I created a state which holds an array of products to buy, this one is filled out when setting the quantity of the product, so once this array has information I passed to the mini-cart component so it can render dynamically its content.
0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord