Replies: 2 comments
-
I haven't tried, but does this hopefully work? const initialObj = {
items: [],
}
Object.defineProperty(initialObj.items, 'total', {
get() { return ... }
})
const state = proxy(initialObj) |
Beta Was this translation helpful? Give feedback.
0 replies
-
Putting all together: const order = proxy({
items: [],
get total() {
return this.items.reduce((total, item) => total + item.subtotal, 0);
},
add(item) {
this.items.push(
{
product: "",
price: 0,
quantity: 0,
get subtotal() {
return this.price * this.quantity;
},
...item
})
}
});
order.add();
order.items[0].product = "xxx";
order.items[0].price = 12.34;
order.items[0].quantity = 4;
order.add();
order.items[1].product = "yyy";
order.items[1].price = 45.67;
order.items[1].quantity = 2;
order.add({
product: "zzz",
price: 78.99,
quantity: 1
});
console.log(order); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello 👋
I have state like this
order.total
is a computed property that depends onitems[].total
. I can't define getter insideitems[]
, how to define computed gettertotal
insideitems[]
?Currently, i define getter for every new entry inside
items[]
Does it's right? or is there have official / best way to handle this?
Beta Was this translation helpful? Give feedback.
All reactions