I had some problems while implementing React Context in my NextJS ecommerce project. When adding products to the cart, everything was fine but when I opened the Cart modal to increase or decrease the quantity of each product before confirming the order, the component didn't re-render. It only re-rendered after I closed the modal. Here is the link to my project on Code sandbox: https://codesandbox.io/s/hopeful-hill-z31o49
Here is the quick demo video of the problem: https://youtu.be/tRD7Hs-9rOw
This is the context file
export const CartContext = createContext<ContextProps>(null);
export const ContextProvider = function ({ children }) {
const [openModal, setOpenModal] = useState(false);
const [cart, setCart] = useState<any[]>([]);
const ctx = {
openModal,
setOpenModal,
cart,
setCart,
};
return <CartContext.Provider value={ctx}>{children}</CartContext.Provider>;
};
The component that had problems while using Context
// ./components/Modal.tsx
const Modal = () => {
const { setOpenModal, cart, setCart } = useContext(CartContext);
console.log(" -> cart", cart);
const totalPrice = cart.reduce(
(sum, currProduct) => sum + currProduct.price * currProduct.quantity,
0,
);
function increaseQuantity(product: any) {
let updatedCart = cart;
const idx = updatedCart.findIndex((p) => p.name === product.name);
updatedCart[idx].quantity += 1;
setCart(updatedCart);
console.log(" -> updatedCart", updatedCart);
}
function decreaseQuantity(product: any) {
let updatedCart = cart;
if (product.quantity > 1) {
const idx = updatedCart.findIndex((p) => p.name === product.name);
updatedCart[idx].quantity -= 1;
setCart(updatedCart);
console.log(" -> updatedCart", updatedCart);
} else {
updatedCart = updatedCart.filter((p) => p.name !== product.name);
}
setCart(updatedCart);
}
return (
<div>
...
{cart.map((product) => (
<div>
<button onClick={() => increaseQuantity(product)}></button>
<div>{product.quantity}</div>
<button onClick={() => decreaseQuantity(product)}></button>
</div>
))}
...
</div>
)