Build a complete shopping cart with add/remove, quantities, and auto-calculating totals!
Click products to add them, adjust quantities - watch totals update automatically!
$999
$699
$199
$399
$149
$79
const cart = createSignal<CartItem[]>([]);Single signal holds all cart items
const totalPrice = createComputed(() =>
ย ย cart.get().reduce((sum, item) =>
ย ย ย ย sum + item.price * item.quantity, 0)
);Totals recalculate automatically when cart changes
import { createSignal, createComputed } from 'signalforge/core';
import { useSignalValue } from 'signalforge/react';
import { useState } from 'react';
function ShoppingCart() {
const [cart] = useState(() => createSignal<CartItem[]>([]));
// Computed totals - auto-update!
const [totalItems] = useState(() => createComputed(() =>
cart.get().reduce((sum, item) => sum + item.quantity, 0)
));
const [totalPrice] = useState(() => createComputed(() =>
cart.get().reduce((sum, item) =>
sum + item.price * item.quantity, 0
)
));
const totalItemsValue = useSignalValue(totalItems);
const totalPriceValue = useSignalValue(totalPrice);
// Add item to cart
const addToCart = (product) => {
const existing = cart.get().find(i => i.id === product.id);
if (existing) {
// Increment quantity
cart.set(cart.get().map(item =>
item.id === product.id
? { ...item, quantity: item.quantity + 1 }
: item
));
} else {
// Add new item
cart.set([...cart.get(), { ...product, quantity: 1 }]);
}
};
// Update quantity
const updateQuantity = (id, delta) => {
cart.set(cart.get()
.map(item => item.id === id
? { ...item, quantity: item.quantity + delta }
: item
)
.filter(item => item.quantity > 0)
);
};
return (
<div>
<h2>Cart ({totalItemsValue} items)</h2>
<h2>Total: 0</h2>
{/* Cart items */}
</div>
);
}Let SignalForge handle totals automatically.
Filter out items with quantity <= 0.
Save cart to localStorage for better UX.